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

Invalid suggestion for explicit_iter_loop, which results in compiler error [trait bounds] #4958

Closed
Luro02 opened this issue Dec 26, 2019 · 0 comments · Fixed by #4978
Closed
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

@Luro02
Copy link

Luro02 commented Dec 26, 2019

Code to reproduce the error

#![deny(clippy::explicit_iter_loop)]

fn takes_iterator<T>(iterator: &T)
where
    for<'a> &'a T: IntoIterator<Item = &'a String>
{
    for i in iterator.into_iter() {
        println!("{}", i);
    }
}


fn main() {
    let iterator = vec!["hello".to_string(), "world".to_string(), "yeah".to_string()];

    dbg!(takes_iterator(&iterator));
}

playground

Output from clippy:

    Checking playground v0.0.1 (/playground)
error: it is more concise to loop over references to containers instead of using explicit iteration methods
 --> src/main.rs:7:14
  |
7 |     for i in iterator.into_iter() {
  |              ^^^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&iterator`
  |
note: lint level defined here
 --> src/main.rs:1:9
  |
1 | #![deny(clippy::explicit_iter_loop)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_iter_loop

error: aborting due to previous error

error: could not compile `playground`.

To learn more, run the command again with --verbose.

Following this suggestion would result in a compiler error:

#![deny(clippy::explicit_iter_loop)]

fn takes_iterator<T>(iterator: &T)
where
    for<'a> &'a T: IntoIterator<Item = &'a String>
{
    for i in &iterator {
        println!("{}", i);
    }
}

playground

   Compiling playground v0.0.1 (/playground)
error[E0277]: `&&T` is not an iterator
 --> src/main.rs:7:14
  |
7 |     for i in &iterator {
  |              ^^^^^^^^^ `&&T` is not an iterator
  |
  = help: the trait `std::iter::Iterator` is not implemented for `&&T`
  = note: required by `std::iter::IntoIterator::into_iter`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

The correct suggestion would be to replace iterator.into_iter() with iterator (which does make sense, because the for loop is syntax sugar for that)

#![deny(clippy::explicit_iter_loop)]

fn takes_iterator<T>(iterator: &T)
where
    for<'a> &'a T: IntoIterator<Item = &'a String>
{
    for i in iterator {
        println!("{}", i);
    }
}

playground

@phansch phansch 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 Dec 26, 2019
bors added a commit that referenced this issue Jan 3, 2020
Fix bad `explicit_into_iter_loop` suggestion

Fixes #4958

changelog: Fix bad `explicit_into_iter_loop` suggestion
@bors bors closed this as completed in ea829bd Jan 3, 2020
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.

2 participants