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

never_loop in for loop #7537

Closed
HKalbasi opened this issue Aug 5, 2021 · 2 comments · Fixed by #7541
Closed

never_loop in for loop #7537

HKalbasi opened this issue Aug 5, 2021 · 2 comments · Fixed by #7541
Assignees
Labels
good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@HKalbasi
Copy link
Member

HKalbasi commented Aug 5, 2021

People use a never_loop for loop for picking first element (if exists) of an iterator. It is possible without such a loop with if let Some(elem) = x.iter().next() { instead of for elem in x { but is it considered a better way? If it isn't, we should disable never_loop for for loops. If it is, we can provide a auto-fix suggestion so people who are not aware of this, don't disable lint.

@camsteffen
Copy link
Contributor

I don't think it should be disabled for for loops. So add a suggestion like this? SGTM.

error: this loop never actually loops
 --> src/main.rs:3:5
  |
3 | /     for first in v {
4 | |         println!("first: {}", first);
5 | |         break;
6 | |     }
  | |_____^

help: try writing

    if let Some(first) = v.into_iter().next() {
        println!("first: {}", first);
    }

@camsteffen camsteffen added good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions labels Aug 6, 2021
@LeSeulArtichaut
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants