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/UnreachableLoop false positive when using conditional next #9746

Closed
Tao-Galasse opened this issue Apr 28, 2021 · 0 comments · Fixed by #9747
Closed

Lint/UnreachableLoop false positive when using conditional next #9746

Tao-Galasse opened this issue Apr 28, 2021 · 0 comments · Fixed by #9747
Labels

Comments

@Tao-Galasse
Copy link

Tao-Galasse commented Apr 28, 2021

In a loop where I conditionally return a value or calling next, rubocop triggers the Lint/UnreachableLoop.

Here is an example :

%w[foo bar tao].each do |name|
  return User.find_by(first_name: name) || next
end

If I have a user named tao but no user named foo or bar, this will run 3 times.


Expected behavior

Not raising a Lint/UnreachableLoop warning.

Actual behavior

W: Lint/UnreachableLoop: This loop will have at most one iteration.
      %w[foo bar tao].each do |name| ...

Steps to reproduce the problem

You can use the example above. You can even replace next by something like (puts 'next'; next) to see it iterate.

RuboCop version

1.13.0


Thanks a lot for your work, you rock! ❤️

@koic koic added the bug label Apr 29, 2021
koic added a commit to koic/rubocop that referenced this issue Apr 29, 2021
Fixes rubocop#9746.

This PR fixes a false positive for `Lint/UnreachableLoop`
when using conditional `next` in a loop.
koic added a commit that referenced this issue Apr 30, 2021
…hable_loop

[Fix #9746] Fix a false positive for `Lint/UnreachableLoop`
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