-
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
Suggest for loop instead of while-let when looping over iterators #396
Conversation
Argh. I just realised that I misread rust-lang/rust#8372 . This issue means that there are cases where |
@@ -53,6 +53,23 @@ fn main() { | |||
while let Some(x) = y { // no error, obviously | |||
println!("{}", x); | |||
} | |||
|
|||
|
|||
while let Option::Some(x) = (1..20).next() { //~ERROR this loop could be written as a `for` loop |
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.
Since these examples depend on a temporary, they are all actually infinite loops 😄
I'd prefer if we used vector iteration here or stored the ranges in an external mutable variable.
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.
I'm not sure what I was thinking there 😆 Thanks, I'll fix that.
599ae33
to
9cf7a73
Compare
I've made fixes for your comments, fixed the false positives where the iterator is used inside the loop body, and rebased the branch onto the current master. Ready for more feedback 😄 |
@@ -6,7 +6,8 @@ A collection of lints to catch common mistakes and improve your Rust code. | |||
[Jump to usage instructions](#usage) | |||
|
|||
##Lints | |||
There are 68 lints included in this crate: | |||
There are 69 lints included in this crate: | |||
There are 65 lints included in this crate: |
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.
This looks wrong.
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.
Looks like I messed up when resolving a conflict for the rebase. Fixed.
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.
Happens to the best of us :-)
Regarding the discussion about iterators being used after the while-let loop:
Ok, I have to eat my words here, I'm a bit stumped. I want to have a function that checks whether the an iterator variable is used again after being used in a while-let loop. I have written a visitor that, given a block, the iterator variable and the id of the while-let loop, checks whether the iterator variable appears after the beginning of the while let loop (i.e. in the loop body or after the loop). But now I need a block to run it on. I thought I could just use |
Ok, I just found |
Note: for tracking uses, try |
Due to rust-lang/rust#8372, we have to use while-let in these cases.
* remove weird infinite loops from compile-tests * remove call to Option::unwrap * in the lint message, show while-let loop rewritten as for loop
d3731e2
to
5ca7ebb
Compare
I've fixed the false positives when an iterator is used after the loop. I've not used let mut iter = 0u32..20;
while let Some(x) = iter().next {}
iter = 1..5;
println!("iter: {:?}" iter); This should strictly speaking warn since it can be written as a for loop, but it currently doesn't since Regarding the |
should work, really. |
@Manishearth That's what I thought, but it doesn't (Probably because of the lifetimes on |
Oh, LateContext has a tcx lifetime. Right. |
Since you had done most of the review, r? @llogiq (If you're busy let me know and I'll do it tomorrow) |
Looks good to me; just slap an |
Done 😄 |
Suggest for loop instead of while-let when looping over iterators
Thanks a bunch! 😃 |
Fixes #387
I think this is ready for review. I even remembered to update the readme for once 😄