Skip to content

Clippy "explicit_iter_loop" suggestion doesn't compile. #2173

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

Closed
SuperCuber opened this issue Oct 25, 2017 · 4 comments
Closed

Clippy "explicit_iter_loop" suggestion doesn't compile. #2173

SuperCuber opened this issue Oct 25, 2017 · 4 comments
Labels
C-bug Category: Clippy is not doing the correct thing good first issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types

Comments

@SuperCuber
Copy link

I have the following code:

const GRID_SIZE: usize = 100;
const BYTES_PER_COLOR: usize = 3;

fn flatten(grid: [[[u8; BYTES_PER_COLOR]; GRID_SIZE]; GRID_SIZE]) -> Vec<u8> {
    let mut flat = Vec::new();

    for row in grid.into_iter() {
        for color in row.iter() {
            flat.extend_from_slice(color);
        }
    }

    flat
}

Clippy gives the following lint:

warning: it is more idiomatic to loop over references to containers instead of using explicit iteration methods
  --> src/pixel_render.rs:33:16
   |
33 |     for row in grid.into_iter() {
   |                ^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&grid`
   |
   = note: #[warn(explicit_iter_loop)] on by default
   = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.166/index.html#explicit_iter_loop

After changing the code to the suggestion, it does not compile:

error[E0277]: the trait bound `&[[[u8; 3]; 100]; 100]: std::iter::Iterator` is not satisfied
  --> src/pixel_render.rs:33:5
   |
33 | /     for row in &grid {
34 | |         for color in row.iter() {
35 | |             flat.extend_from_slice(color);
36 | |         }
37 | |     }
   | |_____^ `&[[[u8; 3]; 100]; 100]` is not an iterator; maybe try calling `.iter()` or a similar method
   |
   = help: the trait `std::iter::Iterator` is not implemented for `&[[[u8; 3]; 100]; 100]`
   = note: required by `std::iter::IntoIterator::into_iter`

The real solution here is grid.iter().

My suggestion: somehow check that the suggestion compiles before making it.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 25, 2017

We should just bail out on arrays with more than 32 elements, they don't implement any traits

My suggestion: somehow check that the suggestion compiles before making it.

That would be nice, but is really really hard

@oli-obk oli-obk added good first issue These issues are a good way to get started with Clippy C-bug Category: Clippy is not doing the correct thing T-middle Type: Probably requires verifiying types labels Oct 25, 2017
@SuperCuber
Copy link
Author

@oli-obk yeah figured so, I guess the alternative is to not have lints that suggest noncompiling code :)

Didn't know arrays bigger than 32 didn't implement traits though, I think that's the thing that can be fixed by introducing integer parameters to generics?

@oli-obk
Copy link
Contributor

oli-obk commented Oct 25, 2017

I guess the alternative is to not have lints that suggest noncompiling code :)

Yea I'm working on automating the detecting of bad suggestions ;) But it's nontrivial. See rust-lang/rust#45516 for some thoughts

I think that's the thing that can be fixed by introducing integer parameters to generics?

jup

@lukasstevens
Copy link
Contributor

lukasstevens commented Oct 25, 2017

Unfortunately I am unable to reproduce this issue with the newest version of clippy. Can anyone confirm that this occurs with the newest version?

EDIT: Never mind, I forgot the #![plugin(clippy)] directive. I am having a look at this issue now.

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 good first issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

3 participants