Skip to content

Commit

Permalink
[Fix #7023] Add auto-correction for Lint/NumberConversion (#7054)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz authored and bbatsov committed May 17, 2019
1 parent ed3b9c8 commit 7cf638c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

* [#7013](https://github.com/rubocop-hq/rubocop/pull/7013): Respect DisabledByDefault for custom cops. ([@XrXr][])
* [#7043](https://github.com/rubocop-hq/rubocop/issues/7043): Prevent RDoc error when installing RuboCop 0.69.0 on Ubuntu. ([@koic][])
* [#7023](https://github.com/rubocop-hq/rubocop/issues/7023): Autocorrection for `Lint/NumberConversion`. ([@Bhacaz][])

## 0.69.0 (2019-05-13)

Expand Down
2 changes: 2 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,8 @@ Lint/NumberConversion:
Description: 'Checks unsafe usage of number conversion methods.'
Enabled: false
VersionAdded: '0.53'
VersionChanged: '0.70'
SafeAutoCorrect: false

Lint/OrderedMagicComments:
Description: 'Checks the proper ordering of magic comments and whether a magic comment is not placed before a shebang.'
Expand Down
7 changes: 7 additions & 0 deletions lib/rubocop/cop/lint/number_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def on_send(node)
end
end

def autocorrect(node)
lambda do |corrector|
corrector.replace(node.loc.expression,
correct_method(node, node.receiver))
end
end

private

def date_time_object?(node)
Expand Down
2 changes: 1 addition & 1 deletion manual/cops_lint.md
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ end

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Disabled | Yes | No | 0.53 | -
Disabled | Yes | Yes (Unsafe) | 0.53 | 0.70

This cop warns the usage of unsafe number conversions. Unsafe
number conversion can cause unexpected error if auto type conversion
Expand Down
31 changes: 31 additions & 0 deletions spec/rubocop/cop/lint/number_conversion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,43 @@
"10".to_i
^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using "10".to_i, use stricter Integer("10", 10).
RUBY

expect_correction(<<~RUBY.strip_indent)
Integer("10", 10)
RUBY
end

it 'when using `#to_i` for integer' do
expect_offense(<<~RUBY)
10.to_i
^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using 10.to_i, use stricter Integer(10, 10).
RUBY

expect_correction(<<~RUBY.strip_indent)
Integer(10, 10)
RUBY
end

it 'when using `#to_f`' do
expect_offense(<<~RUBY)
"10.2".to_f
^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using "10.2".to_f, use stricter Float("10.2").
RUBY

expect_correction(<<~RUBY.strip_indent)
Float("10.2")
RUBY
end

it 'when using `#to_c`' do
expect_offense(<<~RUBY)
"10".to_c
^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using "10".to_c, use stricter Complex("10").
RUBY

expect_correction(<<~RUBY.strip_indent)
Complex("10")
RUBY
end

it 'when `#to_i` called on a variable' do
Expand All @@ -40,6 +56,11 @@
string_value.to_i
^^^^^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using string_value.to_i, use stricter Integer(string_value, 10).
RUBY

expect_correction(<<~RUBY.strip_indent)
string_value = '10'
Integer(string_value, 10)
RUBY
end

it 'when `#to_i` called on a hash value' do
Expand All @@ -48,6 +69,11 @@
params[:id].to_i
^^^^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using params[:id].to_i, use stricter Integer(params[:id], 10).
RUBY

expect_correction(<<~RUBY.strip_indent)
params = { id: 10 }
Integer(params[:id], 10)
RUBY
end

it 'when `#to_i` called on a variable on a array' do
Expand All @@ -56,6 +82,11 @@
args[0].to_i
^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using args[0].to_i, use stricter Integer(args[0], 10).
RUBY

expect_correction(<<~RUBY.strip_indent)
args = [1,2,3]
Integer(args[0], 10)
RUBY
end
end

Expand Down

0 comments on commit 7cf638c

Please sign in to comment.