Skip to content

Commit

Permalink
Performance/FirstLastAfterFilter: ignore .first/.last with args
Browse files Browse the repository at this point in the history
closes #104
  • Loading branch information
veelenga committed Jun 3, 2019
1 parent 9e2061c commit 037d1dd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
7 changes: 7 additions & 0 deletions spec/ameba/rule/performance/first_last_after_filter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
1 change: 1 addition & 0 deletions src/ameba/rule/performance/first_last_after_filter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit 037d1dd

Please sign in to comment.