Skip to content

False positive in needless_range_loop #2036

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
chyvonomys opened this issue Sep 9, 2017 · 4 comments
Closed

False positive in needless_range_loop #2036

chyvonomys opened this issue Sep 9, 2017 · 4 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.

Comments

@chyvonomys
Copy link
Contributor

Clippy reports needless_range_loop and suggests using iter/take/skip which is wrong for this case:

let ns = [2, 3, 5, 7];
for i in 3..10 {
    println!("{}", ns[i % 4]);
}
warning: the loop variable `i` is only used to index `ns`.
 --> src\main.rs:3:5
  |
3 | /     for i in 3..10 {
4 | |         println!("{}", ns[i % 4]);
5 | |     }
  | |_____^
  |
  = note: #[warn(needless_range_loop)] on by default
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.158/index.html#needless_range_loop
help: consider using an iterator
  |
3 |     for <item> in ns.iter().take(10).skip(3) {
  |         ^^^^^^
@chyvonomys
Copy link
Contributor Author

Looks like this is related to this piece:
https://github.com/rust-lang-nursery/rust-clippy/blob/9c9a4953c3f131a8481f15f7e7a805972056a084/clippy_lints/src/loops.rs#L1346-L1354

While same_var(...) will correctly catch cases like: ns[i], second case catch things like ns[i % 4].

Code was introduced with this PR: #2021

@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 labels Sep 10, 2017
@oli-obk
Copy link
Contributor

oli-obk commented Sep 10, 2017

Jup, this should probably suggest ns[3..7].iter().cycle().take(10)

@chyvonomys
Copy link
Contributor Author

I think it would be better to make it work as before, i.e. to report cases like ns[i] only. Expression under [...] may be more complex than just i % 4 and figuring out what iterator style equivalent to suggest seems to be a non-trivial task. Also, using just expression is better than iterator. At least in my example.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 10, 2017

fixed in #2118

@oli-obk oli-obk closed this as completed Oct 10, 2017
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.
Projects
None yet
Development

No branches or pull requests

2 participants