-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Improve suggestions for needless_range_loop
#1864
Comments
Also another case I stumbled across: for i in 1..field.len() {
let to_insert = field[i];
let mut j: usize = i;
while j >= 1 && field[j - 1] > to_insert {
field[j] = field[j - 1];
j -= 1;
}
field[j] = to_insert;
} This is an implementation of insertionsort. I'm new to Rust so there might be a way that I don't know of yet but in this case, clippy suggests the following: The problem is that in the while loop, I assign to another field in my array, that is not |
Thanks! You're right, we should totally fix those suggestions |
for the first case, clippy now suggest something that doesn't compile: fn main() {
let mut arr = vec![1, 2, 3];
for i in 1..arr.len()-1 {
arr[i] = 6;
}
}
applying the suggestion: fn main() {
let mut arr = vec![1, 2, 3];
for item in arr.iter_mut().take(arr.len()-1).skip(1) {
*item = 6
}
}
|
Hey there. First of all: Great work to all the devs. Clippy is a real handy tool when developing in Rust.
Now about my proposal: the
needless_range_loop
gives pretty good suggestions even for non default ranges. But if a range loop like the following is linted, one could suggest to use a mutable iterator usingiter_mut()
:Clippy would suggest something like
for <item> in arr.iter().take(arr.len()-1).skip(1) {
but in this caseiter_mut()
must be used to assign new values to<item>
. I don't know if there is an easy way to detect cases like this one but I think it would be a great improvement.The text was updated successfully, but these errors were encountered: