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

Lint/NextWithoutAccumulator is reported incorrectly for a nested loop #3308

Closed
leonid-shevtsov opened this issue Jul 11, 2016 · 0 comments
Closed

Comments

@leonid-shevtsov
Copy link

Expected behavior

Having a reduce loop and another loop inside of it, it's normal to next without an argument from the nested loop. It's only wrong to next without an argument from the reduce loop.

Actual behavior

Lint/NextWithoutAccumulator is reported.

Steps to reproduce the problem

Example code to reproduce:

result = [[1, 2, 3], [4, 5, 6]].inject([]) do |acc, elems|
  elems.each_with_index do |elem, i|
    next if i == 1
    acc << elem
  end
  acc
end

puts result

gets us

Inspecting 1 file
W

Offenses:

repro.rb:3:5: W: Lint/NextWithoutAccumulator: Use next with an accumulator argument in a reduce.
    next if i == 1
    ^^^^

1 file inspected, 1 offense detected

RuboCop version

$ rubocop -V
0.37.2 (using Parser 2.3.0.6, running on ruby 2.1.2 x86_64-darwin14.0)```
Drenmi added a commit to Drenmi/rubocop that referenced this issue Jul 14, 2016
… enumeration

This cop would register an offense if a nested block contained a `next`
without an accumulator, e.g.:

```
[(1..3), (4..6)].reduce(0) do |acc, elems|
  elems.each_with_index do |elem, i|
    next if i == 1
    acc << elem
  end
  acc
end
```

This fix addresses that, by checking if the direct block parent of any
offending `next` is indeed the block that we were inspecting.
Neodelf pushed a commit to Neodelf/rubocop that referenced this issue Oct 15, 2016
… enumeration (rubocop#3313)

This cop would register an offense if a nested block contained a `next`
without an accumulator, e.g.:

```
[(1..3), (4..6)].reduce(0) do |acc, elems|
  elems.each_with_index do |elem, i|
    next if i == 1
    acc << elem
  end
  acc
end
```

This fix addresses that, by checking if the direct block parent of any
offending `next` is indeed the block that we were inspecting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant