Skip to content

Commit

Permalink
[Fix rubocop#9907] Fix an incorrect auto-correct for Lint/UselessTimes
Browse files Browse the repository at this point in the history
Fixes rubocop#9907.

This PR fixes an incorrect auto-correct for `Lint/UselessTimes`
when using block argument for `1.times`.
  • Loading branch information
koic committed Jul 3, 2021
1 parent d1e8f9b commit 64d06c6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
@@ -0,0 +1 @@
* [#9907](https://github.com/rubocop/rubocop/issues/9907): Fix an incorrect auto-correct for `Lint/UselessTimes` when using block argument for `1.times`. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/useless_times.rb
Expand Up @@ -81,7 +81,7 @@ def autocorrect_block(corrector, node)
return if block_reassigns_arg?(node, block_arg)

source = node.body.source
source.gsub!(/\b#{block_arg}\b/, '1') if block_arg
source.gsub!(/\b#{block_arg}\b/, '0') if block_arg

corrector.replace(node, fix_indentation(source, node.loc.column...node.body.loc.column))
end
Expand Down
8 changes: 4 additions & 4 deletions spec/rubocop/cop/lint/useless_times_spec.rb
Expand Up @@ -55,7 +55,7 @@
RUBY

expect_correction(<<~RUBY)
something(1)
something(0)
RUBY
end

Expand Down Expand Up @@ -163,8 +163,8 @@ def foo
RUBY

expect_correction(<<~RUBY)
do_something(1)
do_something_else(1)
do_something(0)
do_something_else(0)
RUBY
end

Expand Down Expand Up @@ -205,7 +205,7 @@ def foo
RUBY

expect_correction(<<~RUBY)
do_something(1)
do_something(0)
j = 1
do_something_else(j)
RUBY
Expand Down

0 comments on commit 64d06c6

Please sign in to comment.