Performance/FirstLastAfterFilter: ignore .first/.last with args

closes #104
This commit is contained in:
Vitalii Elenhaupt 2019-06-03 18:25:27 +03:00
parent 9e2061cbf6
commit 037d1dde74
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
2 changed files with 8 additions and 0 deletions

View file

@ -36,6 +36,13 @@ module Ameba::Rule::Performance
subject.catch(source).should_not be_valid
end
it "does not report if there is selected followed by first with arguments" do
source = Source.new %(
[1, 2, 3].select { |n| n % 2 == 0 }.first(2)
)
subject.catch(source).should be_valid
end
it "reports if there is select followed by first?" do
source = Source.new %(
[1, 2, 3].select { |e| e > 2 }.first?

View file

@ -45,6 +45,7 @@ module Ameba::Rule::Performance
def test(source, node : Crystal::Call)
return unless CALL_NAMES.includes?(node.name) && (obj = node.obj)
return if node.args.any?
if node.block.nil? && obj.is_a?(Crystal::Call) &&
filter_names.includes?(obj.name) && !obj.block.nil?