-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Weird warning for needless_range_loop #398
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
Comments
cc @birkenfeld |
This only looks strange, but this lint doesn't check the range end expression at all. It only sees that you're indexing In this case, the iteratee could be |
SGTM |
@birkenfeld Well, I made the example simpler, but I my original code, both the Here is my example: fn levenshtein_distance(string1: &str, string2: &str) -> usize {
fn distance(i: usize, j: usize, d: &[Vec<usize>], string1: &str, string2: &str) -> usize {
match (i, j) {
(i, 0) => i,
(0, j) => j,
(i, j) => {
let delta =
if string1.chars().nth(i - 1) == string2.chars().nth(j - 1) {
0
}
else {
1
};
*[
d[i - 1][j] + 1,
d[i][j - 1] + 1,
d[i - 1][j - 1] + delta
].iter().min().unwrap()
},
}
}
let mut d = vec![];
for i in 0 .. string1.len() + 1 {
d.push(vec![]);
for j in 0 .. string2.len() + 1 {
let dist = distance(i, j, &d, string1, string2);
d[i].push(dist);
}
}
d[string1.len()][string2.len()]
}
for i in 0 .. string1.len() + 1 { but I could not iterate over the vector since it is empty. |
Is this a bug? |
Just adding onto this: I got this error for something along the lines of:
And this can't be replaced by a simple |
@Undeterminant do you know about |
@Mccarton: I did not at the time, although the point was that the lint's suggestion of just using enumerate wasn't good enough. |
@Undeterminant this is possible to write with
I'm beginning to think that |
Agree. This lint doesn't seem to check what else you're doing with the range variable in the loop. While it can be replaced by using Simple example:
vs
2nd is needlessly more verbose. |
@antoyo I've put the example you mentioned at #398 (comment) to the test, and Clippy no longer issues a lint: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=d0fd172ad188002310d57c72846a32f7 Does this mean that this issue has been resolved? Lemme know if I tested it alright :) |
While that example seems fixed, the example in the original post still trigger a warning. |
Just so that we share the same clippy and code, here's a playground with the original example: So... indeed, the program crashes when the vector contains less elements than the string. That's not good. This lint's suggestion should, I believe, prevent that kind of crash from these kinds of loop all the time. Anyways. I think the best way to analyze this lint's suggestion is to see what happens case by case, and see if this matches the intent of the person who wrote the code being linted:
What do you think, @antoyo? In this particular example, the suggestion seems very reasonable to me. Could there be a more elaborate counterexample that triggers an incorrect suggestion? |
Hello.
I found a weird warning for a range loop.
In the following code:
I get the following warning:
This is wrong since I am iterating over the
string
, not thevec
.If I remove the line using the
vec
, the warning is gone.Thanks to fix this issue.
(By the way, is there an option to span errors instead of warnings in clippy?)
The text was updated successfully, but these errors were encountered: