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

explicit_counter_loop suggests enumerate when it does not exist #4678

Closed
ChrisJefferson opened this issue Oct 16, 2019 · 4 comments · Fixed by #4691
Closed

explicit_counter_loop suggests enumerate when it does not exist #4678

ChrisJefferson opened this issue Oct 16, 2019 · 4 comments · Fixed by #4691
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@ChrisJefferson
Copy link

In the following code:

fn checky(x: Vec<isize>) {
    let mut pos = 0;
    for i in x {
        println!("{}:{}\n", i, pos);
        pos += 1;
    }
}

clippy suggests:

warning: the variable `pos` is used as a loop counter.
   --> src/card.rs:273:14
    |
273 |     for i in x {
    |              ^ help: consider using: `for (pos, i) in x.enumerate()`
    |
    = note: `#[warn(clippy::explicit_counter_loop)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_counter_loop

But, if I try that, I get:

error[E0599]: no method named `enumerate` found for type `std::vec::Vec<isize>` in the current scope
   --> src/card.rs:273:23
    |
273 |     for (pos, i) in x.enumerate() {
    |                       ^^^^^^^^^
    |
    = note: the method `enumerate` exists but the following trait bounds were not satisfied:
            `&mut [isize] : std::iter::Iterator`
            `&mut std::vec::Vec<isize> : std::iter::Iterator`
@flip1995 flip1995 added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Oct 16, 2019
@flip1995
Copy link
Member

We should suggest iter().enumerate() here, if the expression doesn't satisfy the Iterator bound.

@ChrisJefferson
Copy link
Author

My rust isn't perfect, but in at least one place I had to use into_iter(), rather than iter(), to get code which compiled and ran correctly.

@flip1995
Copy link
Member

Oh yeah, to be more specific, we need to suggest on of iter(), iter_mut(), into_iter() before the enumerate()

@HMPerson1
Copy link
Contributor

I can work on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants