Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #421] Fix false positives for Performance/TimesMap #423

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/rubocop/cop/performance/times_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def on_block(node)

def check(node)
times_map_call(node) do |map_or_collect, count|
next unless handleable_receiver?(node)

add_offense(node, message: message(map_or_collect, count)) do |corrector|
replacement = "Array.new(#{count.source}#{map_or_collect.arguments.map { |arg| ", #{arg.source}" }.join})"

Expand All @@ -58,6 +60,13 @@ def check(node)
end
end

def handleable_receiver?(node)
receiver = node.receiver.receiver
return true if receiver.literal? && (receiver.int_type? || receiver.float_type?)

node.receiver.dot?
end

def message(map_or_collect, count)
template = if count.literal?
"#{MESSAGE}."
Expand Down
42 changes: 42 additions & 0 deletions spec/rubocop/cop/performance/times_map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,48 @@
end
end

context 'with a block with safe navigation call for integer literal' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY, method: method)
42&.times&.#{method} { |i| i.to_s }
^^^^^^^^^^^^{method}^^^^^^^^^^^^^^^ Use `Array.new(42)` with a block instead of `.times.#{method}`.
RUBY
end
end

context 'with a block with safe navigation call for float literal' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY, method: method)
4.2&.times&.#{method} { |i| i.to_s }
^^^^^^^^^^^^^{method}^^^^^^^^^^^^^^^ Use `Array.new(4.2)` with a block instead of `.times.#{method}`.
RUBY
end
end

context 'with a block with safe navigation call for nil literal' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, method: method)
nil&.times&.#{method} { |i| i.to_s }
RUBY
end
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this case?

it 'does not register an offense and corrects' do
  expect_no_offenses(<<~RUBY, method: method)
    foo&.times&.#{method} { |i| i.to_s }
  RUBY
end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I've fixed that case.

context 'with a block with safe navigation call for local variable' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, method: method)
nullable&.times&.#{method} { |i| i.to_s }
RUBY
end
end

context 'with a block with safe navigation call for instance variable' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, method: method)
@nullable&.times&.#{method} { |i| i.to_s }
RUBY
end
end

context 'for non-literal receiver' do
it 'registers an offense' do
expect_offense(<<~RUBY, method: method)
Expand Down