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

Style/For correction breaks when using array concatenation #10651

Closed
JasonBarnabe opened this issue May 19, 2022 · 1 comment · Fixed by #10654
Closed

Style/For correction breaks when using array concatenation #10651

JasonBarnabe opened this issue May 19, 2022 · 1 comment · Fixed by #10654

Comments

@JasonBarnabe
Copy link

a = [1]
b = [2]
c = [3]

for n in a + b + c
  puts n
end

Outputs:

1
2
3

Running rubocop -A on this method:

style_for_test.rb:5:1: C: [Corrected] Style/For: Prefer each over for.
for n in a + b + c ...
^^^^^^^^^^^^^^^^^^

File becomes:

a = [1]
b = [2]
c = [3]

a + b + c.each do |n|
  puts n
end

Outputs:

3

Output has changed!

Expected correction:

a = [1]
b = [2]
c = [3]

(a + b + c).each do |n|
  puts n
end

Which retains the original output.

rubocop -V:

1.29.1 (using Parser 3.1.2.0, rubocop-ast 1.18.0, running on ruby 2.7.6 x86_64-linux)
  - rubocop-minitest 0.17.2
  - rubocop-performance 1.13.2
  - rubocop-rails 2.13.2
@nobuyo
Copy link
Contributor

nobuyo commented May 20, 2022

I'll attempt to fix this.

nobuyo added a commit to nobuyo/rubocop that referenced this issue May 23, 2022
…with operator methods as collection

Update lib/rubocop/cop/correctors/for_to_each_corrector.rb

Co-authored-by: Koichi ITO <koic.ito@gmail.com>

Remove unnecessary methods
koic added a commit that referenced this issue May 23, 2022
[Fix #10651] Fix autocorrect for `Style/For` when using array with operator methods as collection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants