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

1.times for block with argument incorrectly auto-corrected #9907

Closed
msbit opened this issue Jul 3, 2021 · 2 comments · Fixed by #9908
Closed

1.times for block with argument incorrectly auto-corrected #9907

msbit opened this issue Jul 3, 2021 · 2 comments · Fixed by #9908
Labels

Comments

@msbit
Copy link

msbit commented Jul 3, 2021

With the following code:

1.times do |i|
  puts i
end

which, when run, outputs: 0

if considered by rubocop with --auto-correct-all enabled, produces the following auto-corrected code:

# frozen_string_literal: true

puts 1

which, when run, outputs: 1


Expected behavior

Auto-correct use of 1.times to block invocation with 0 as the argument.

Actual behavior

Auto-correct use of 1.times to block invocation with 1 as the argument.

Steps to reproduce the problem

  • Create Ruby file containing:
1.times do |i|
  puts i
end
  • Run rubocop --auto-correct-all <file.rb>

RuboCop version

$ rubocop -V
1.18.2 (using Parser 3.0.1.1, rubocop-ast 1.7.0, running on ruby 2.7.3 x86_64-darwin19)
@msbit
Copy link
Author

msbit commented Jul 3, 2021

Could be as simple as a change to the replacement at

source.gsub!(/\b#{block_arg}\b/, '1') if block_arg
?

@koic
Copy link
Member

koic commented Jul 3, 2021

Yep. I've opened #9908. Thank you.

koic added a commit to koic/rubocop that referenced this issue Jul 3, 2021
Fixes rubocop#9907.

This PR fixes an incorrect auto-correct for `Lint/UselessTimes`
when using block argument for `1.times`.
@koic koic closed this as completed in #9908 Jul 4, 2021
koic added a commit that referenced this issue Jul 4, 2021
…_useless_time

[Fix #9907] Fix an incorrect auto-correct for `Lint/UselessTimes`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants