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

False Positive: needless_range_loop #2277

Closed
ghost opened this issue Dec 18, 2017 · 0 comments · Fixed by #5798
Closed

False Positive: needless_range_loop #2277

ghost opened this issue Dec 18, 2017 · 0 comments · Fixed by #5798
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@ghost
Copy link

ghost commented Dec 18, 2017

This example code produces the clippy error below.

pub fn example(list: &[[f64; 3]]) {
    let mut x: [f64; 3] = [10.; 3];

    for i in 0..3 {
        x[i] = list
            .iter()
            .map(|item| item[i])
            .sum::<f64>();
    }
}
warning: the loop variable `i` is only used to index `x`.
 --> src/lib.rs:4:5
  |
4 | /     for i in 0..3 {
5 | |         x[i] = list
6 | |             .iter()
7 | |             .map(|item| item[i])
8 | |             .sum::<f64>();
9 | |     }
  | |_____^
  |
  = note: #[warn(needless_range_loop)] on by default
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.176/index.html#needless_range_loop
help: consider using an iterator
  |
4 |     for <item> in x.iter_mut().take(3) {
  |

The warning is incorrect. i is used in the closure within the map call. The suggestion won't work because of that.

@oli-obk oli-obk added the C-bug Category: Clippy is not doing the correct thing label Dec 18, 2017
bors added a commit that referenced this issue Jul 14, 2020
Add test for `needless_range_loop` issue

Closes #2277

This was fixed when we fixed #2542.

changelog: none
@bors bors closed this as completed in d067d03 Jul 14, 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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant