Skip to content

Commit

Permalink
[Fix rubocop#8835] Fix an incorrect autocorrect for `Style/RedundantI…
Browse files Browse the repository at this point in the history
…nterpolation`

Fixes rubocop#8835.

This PR fixes an incorrect autocorrect for `Style/RedundantInterpolation`
when using string interpolation for non-operator methods.
  • Loading branch information
koic committed Oct 5, 2020
1 parent 4f101b6 commit 273bb65
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@
* [#8807](https://github.com/rubocop-hq/rubocop/pull/8807): Fix a false positive for `Style/RedundantCondition` when using assignment by hash key access. ([@koic][])
* [#8848](https://github.com/rubocop-hq/rubocop/issues/8848): Fix a false positive for `Style/CombinableLoops` when using the same method with different arguments. ([@dvandersluis][])
* [#8843](https://github.com/rubocop-hq/rubocop/issues/8843): Fix an incorrect autocorrect for `Lint/AmbiguousRegexpLiteral` when sending method to regexp literal receiver. ([@koic][])
* [#8835](https://github.com/rubocop-hq/rubocop/issues/8835): Fix an incorrect autocorrect for `Style/RedundantInterpolation` when using string interpolation for non-operator methods. ([@koic][])

### Changes

Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/cop/style/redundant_interpolation.rb
Expand Up @@ -51,7 +51,12 @@ def single_interpolation?(node)
end

def single_variable_interpolation?(node)
node.children.one? && variable_interpolation?(node.children.first)
return false unless node.children.one?

first_child = node.children.first

variable_interpolation?(first_child) ||
first_child.send_type? && !first_child.operator_method?
end

def interpolation?(node)
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/style/redundant_interpolation_spec.rb
Expand Up @@ -157,6 +157,28 @@
RUBY
end

it 'registers an offense for "#{number}"' do
expect_offense(<<~'RUBY')
"#{number}"
^^^^^^^^^^^ Prefer `to_s` over string interpolation.
RUBY

expect_correction(<<~'RUBY')
number.to_s
RUBY
end

it 'registers an offense for "#{do_something(42)}"' do
expect_offense(<<~'RUBY')
"#{do_something(42)}"
^^^^^^^^^^^^^^^^^^^^^ Prefer `to_s` over string interpolation.
RUBY

expect_correction(<<~'RUBY')
do_something(42).to_s
RUBY
end

it 'registers an offense for "#{var}"' do
expect_offense(<<~'RUBY')
var = 1; "#{var}"
Expand Down

0 comments on commit 273bb65

Please sign in to comment.