-
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
New lint: while let Some(&x) = slice.iter().next()
is infinite loop
#4554
Comments
Basically calling |
I'd love to contribute to Clippy. Since this is a good first issue I was hoping I could start working on it. Could I get some pointers on where in the code to get started? |
Cool! I usually start by writing a test case (create a new .rs file in |
I'm a little late, but this doc gives you step by step instructions on how to write a new lint. For this lint the Author lint chapter is probably interesting for you. |
Hi all, if @pop is not going to tackle this issue, I'd like to take it. I've already taken a look at the resources, thanks @llogiq and @flip1995. rust-clippy/clippy_lints/src/loops.rs Line 560 in 54bf4ff
This "desugars" the while let into a loop match, which in our case would be:
into
Afterwards it decomposes the first arm using the pattern kind and the match_expre using the expression kind as well, rust-clippy/clippy_lints/src/loops.rs Lines 561 to 566 in 54bf4ff
So that, roughly speaking:
Where I get lost is how to get the type information from the I'll keep looking into it, but if you have any useful pointers into what I should look into (HIR, MIR, Ty) or examples I would greatly appreciate it, specially since I'm new to clippy and rustc internals. |
You seem primed and ready to take this one, so go for it @chibby0ne. |
Seems good to me
Exactly, you should only have to look at the
You should be able to use the function rust-clippy/clippy_lints/src/utils/mod.rs Lines 178 to 187 in 737f0a6
You can grep the repo for |
Thanks @pop I appreciate! 😄 |
How did you fare with this one, @chibby0ne? :) |
I started to look into this issue. But it seems that it contradicts #1654. There is also an example where recreating the iterator is actually useful: use std::collections::HashSet;
let mut values = HashSet::new();
values.insert(1);
while let Some(..) = values.iter().next() {
values.remove(&1);
} Also see rust-clippy/clippy_lints/src/loops/while_let_on_iterator.rs Lines 26 to 34 in ddc2598
|
This is also useful for streams, it's very easy to miss in streams that wait for events because they behave almost identically and manifest in subtle ways, I recently came across this: #[tokio::main]
async fn main() -> anyhow::Result<()> {
- while let Some(mode) = dark_light::subscribe().await?.next().await {
+ let mut stream = dark_light::subscribe().await?;
+ while let Some(mode) = stream.next().await {
println!("System theme changed: {:?}", mode);
} |
This code is an infinite loop: (Do not run this in playpen)
The text was updated successfully, but these errors were encountered: