-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closes #1219 false positive for explicit_counter_loop #3135
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
Closes #1219 false positive for explicit_counter_loop #3135
Conversation
It looks like the integration test for RLS timed out after exactly 10 minutes. Looking at other builds it seems that test commonly runs long, can we re-run the build or is there something I should look into? |
Restarted the RLS build, sometimes they just don't start. I'm not really sure why the false positive is not triggered anymore. fn main() {
let text = "banana";
let mut count = 0;
for ch in text.chars() {
if ch == 'a' {
println!("abc")
}
count += 1
}
println!("{}", count);
} And it doesn't: https://play.rust-lang.org/?gist=8081f695a48d99e0fe4e3e127b2cf781&version=nightly&mode=debug&edition=2015 |
It looks like we have very low test coverage for that lint. I only found these two tests that trigger the lint: |
Maybe the lint is allow-by-default and needs a |
The playground link by @phansch includes an explicit I also tried the other test cases listed above and they aren't triggering the lint in the playground either. Testing locally, I found that the following DO trigger the lint:
while these DO NOT trigger the lint:
I'll take a look and try to figure out why the lint isn't being triggered locally. For the playground, does clippy typically work normally in the playground? |
Alright, I've narrowed it down a bit. So running locally, the following does not trigger the lint. But if I remove the
|
Ah so the lint doesn't get triggered if the count variable gets used after the loop, which makes sense. If we would define the count variable in the loop decl, it would not be usable after the loop body. Also adding Yep: #473 |
This commit corrects the bug where using the variable after the loop prevents the lint from triggering, and adds some test cases to catch it. I have not yet checked to make sure this doesn't cause regressions elsewhere though. Unless someone else gets to it first, I'll check that tonight and then finally try to tackle the original issue (false positive when there is a continue in the loop). |
This wasn't a bug, but intended. See PR #473 and corresponding issues #384 and #373 The only fn main() {
let text = "banana";
let mut count = 0;
for ch in text.chars() {
if ch == 'a' {
continue;
}
count += 1
}
} But if you don't use the count variable after the loop, I don't think this is a FP, because you can use |
Thanks for setting me straight here, I misread your first post but this is obvious in hindsight.
So this issue can be closed without changing the code then? Or perhaps we just add some tests here to ensure the appropriate coverage and link them to these issues? |
Yes, I think this issue is resolved. But adding some tests for future reference is always good. It would be great if you could do this! |
I've updated the tests, but looking at it again I'm thinking there is still a false positive here. Modifying the example code from @flip1995 slightly:
Currently this triggers the lint, but using enumerate here would change the behavior. @flip1995 can you confirm I am thinking about this correctly? |
Yes this is a FP! Playground EDIT (When should the lint not get triggered in case of a
Since the lint won't get triggered if the counter is used after the loop, the only thing to add would be to bail out if Does this make sense to you? Or do you think I missed something? |
That makes sense. So I think we'd need one additional test case.
I'll try implementing a fix for this, and follow up if I run into any additional issues. Thanks for your support! |
I've corrected the behavior we discussed above, as well adding two more tests and corrected handling of nested loops as shown below.
If all this looks good, I believe this is fit to be merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Hello,
As in #3131, I added a test case here based on the reported issue #1219, and I'm not seeing a test failure. I am new to the clippy codebase, so please let me know if I am just missing something obvious.
Thanks!