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 an incorrect auto-correct for Lint/NumberConversion #9633

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#9633](https://github.com/rubocop/rubocop/pull/9633): Fix an incorrect auto-correct for `Lint/NumberConversion` when `to_i` method in symbol form. ([@koic][])
7 changes: 7 additions & 0 deletions lib/rubocop/cop/lint/number_conversion.rb
Expand Up @@ -105,6 +105,8 @@ def handle_as_symbol(node)
corrected_method: correct_sym_method(to_method)
)
add_offense(node, message: message) do |corrector|
remove_parentheses(corrector, node) if node.parenthesized?

corrector.replace(sym_node, correct_sym_method(to_method))
end
end
Expand All @@ -120,6 +122,11 @@ def correct_sym_method(to_method)
"{ |i| #{body} }"
end

def remove_parentheses(corrector, node)
corrector.replace(node.loc.begin, ' ')
corrector.remove(node.loc.end)
end
Comment on lines +125 to +128
Copy link
Member

Choose a reason for hiding this comment

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

We have two other remove_parentheses methods defined on various cops (although they work slightly differently in each case) maybe it'd be good to normalize that at some point.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah. I didn't normalize here because each one has different behavior, but I'll look at it separately :-)


def ignore_receiver?(receiver)
if receiver.send_type? && ignored_method?(receiver.method_name)
true
Expand Down
17 changes: 14 additions & 3 deletions spec/rubocop/cop/lint/number_conversion_spec.rb
Expand Up @@ -131,7 +131,18 @@
RUBY

expect_correction(<<~RUBY)
"1,2,3,foo,5,6,7,8".split(',').map({ |i| Integer(i, 10) })
"1,2,3,foo,5,6,7,8".split(',').map { |i| Integer(i, 10) }
RUBY
end

it 'registers offense and autocorrects without parentheses' do
expect_offense(<<~RUBY)
"1,2,3,foo,5,6,7,8".split(',').map &:to_i
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using &:to_i, use stricter { |i| Integer(i, 10) }.
RUBY

expect_correction(<<~RUBY)
"1,2,3,foo,5,6,7,8".split(',').map { |i| Integer(i, 10) }
RUBY
end

Expand All @@ -142,7 +153,7 @@
RUBY

expect_correction(<<~RUBY)
"foo".try({ |i| Float(i) })
"foo".try { |i| Float(i) }
RUBY
end

Expand All @@ -153,7 +164,7 @@
RUBY

expect_correction(<<~RUBY)
"foo".send({ |i| Complex(i) })
"foo".send { |i| Complex(i) }
RUBY
end
end
Expand Down