Skip to content

Commit

Permalink
Fix a false positive for Lint/NumberConversion
Browse files Browse the repository at this point in the history
This PR fixes the following false positive and incorrect auto-correct
for `Lint/NumberConversion` when `:to_f` is one of multiple method arguments.

```console
% cat example.rb
delegate :to_f, to: :description, allow_nil: true

% bundle exec rubocop --only Lint/NumberConversion -A example.rb
(snip)

Inspecting 1 file
W

Offenses:

example.rb:1:1: W: [Corrected] Lint/NumberConversion: Replace unsafe
number conversion with number class parsing, instead of using :to_f, use
stricter { |i| Float(i) }.
delegate :to_f, to: :description, allow_nil: true
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

% cat example.rb
delegate { |i| Float(i) }, to: :description, allow_nil: true

% ruby -c example.rb
example.rb:1: syntax error, unexpected ',', expecting end-of-input
delegate { |i| Float(i) }, to: :description, allow_nil:...
```
  • Loading branch information
koic authored and bbatsov committed May 2, 2021
1 parent 9d6a853 commit baba552
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
@@ -0,0 +1 @@
* [#9757](https://github.com/rubocop/rubocop/pull/9757): Fix a false positive for `Lint/NumberConversion` when `:to_f` is one of multiple method arguments. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/number_conversion.rb
Expand Up @@ -97,7 +97,7 @@ def handle_conversion_method(node)

def handle_as_symbol(node)
to_method_symbol(node) do |receiver, sym_node, to_method|
next if receiver.nil?
next if receiver.nil? || !node.arguments.one?

message = format(
MSG,
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/lint/number_conversion_spec.rb
Expand Up @@ -121,6 +121,12 @@
to_i
RUBY
end

it 'when `:to_f` is one of multiple method arguments' do
expect_no_offenses(<<~RUBY)
delegate :to_f, to: :description, allow_nil: true
RUBY
end
end

context 'to_method in symbol form' do
Expand Down

0 comments on commit baba552

Please sign in to comment.