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

Wrong hint about needless_range_loop #878

Closed
BusyJay opened this issue Apr 25, 2016 · 9 comments
Closed

Wrong hint about needless_range_loop #878

BusyJay opened this issue Apr 25, 2016 · 9 comments
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have T-middle Type: Probably requires verifiying types

Comments

@BusyJay
Copy link

BusyJay commented Apr 25, 2016

Clippy warns about following snippet:

let v = vec![5; 10];
for i in 0..v.len() - 2 {
    println!("{}", v[i + 2] - v[i]);
}

It suggest me to use enumerate. But I don't think I can use enumerate in this situation.

@Manishearth
Copy link
Member

.enumerate().first(v.len()-2)

It looks more complicated but you don't have to worry about bounds checks.

@BusyJay
Copy link
Author

BusyJay commented Apr 25, 2016

No, I need to access two items at different position. In this case, I need to access i + 2 and i.

@oli-obk
Copy link
Contributor

oli-obk commented Apr 25, 2016

I think the proper way would be something like

for (&a, &b) in v[2..].iter().zip(&v) {
    println!("{}", a - b);
}

@Manishearth
Copy link
Member

(While there are ways of doing this with iterators, the hint should still be improved)

@BusyJay
Copy link
Author

BusyJay commented Apr 25, 2016

@oli-obk I think my version is more clean and simple. :)

@oli-obk
Copy link
Contributor

oli-obk commented Apr 25, 2016

@BusyJay you need to specify the 2 two times ;) mine is less readable for sure, but that's mainly b/c we don't have good ways to zip like for (&a, &b) in (&v[2..], &v) {}

@BusyJay
Copy link
Author

BusyJay commented May 20, 2016

Maybe windows is a good choice in this situation.

@oli-obk oli-obk added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. C-bug Category: Clippy is not doing the correct thing T-middle Type: Probably requires verifiying types labels May 10, 2017
@phansch phansch added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 21, 2020
@y21
Copy link
Member

y21 commented Jun 14, 2023

Is this issue still relevant today? It doesn't seem to lint on the code provided anymore: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ac000dbc5a08cd445b4babe76f6176bf

@Manishearth
Copy link
Member

Yeah that lint has gotten tons of improvement but we probably had duplicate issues filed. Nice catch!

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 E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

5 participants