-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
while_let_on_iterator should not trigger on borrowed iterators if the iterator is subsequently used. #7659
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
It's hard to call this one a false positive as the suggestion is correct in that you can use a As an aside I would be writing the code more like this (if you like combinators): let mut it = tokens.into_iter();
let value: TokenStream = (&mut it).take_while(|x| match x {
TokenTree::Punct(p) => p.as_char() != ',',
_ => true,
}).collect();
Ok((ValueExpr::parse.parse2(value)?, it.collect())) |
Looks like clippy is correct there. You can use |
yeah, i deleted my comment when i realised (some half an hour later...). i'm not sure if the lint suggestion is more obvious from the cli, but it really wasn't through lsp-mode |
@jackh726 My point about it being better style not to use a let value: TokenStream = (&mut it)
.take_while(|x| !matches!(x, TokenTree::Punct(p) if p.as_char() == ','))
.collect(); |
I think by_ref can be used as an alternative to
|
@mikerite There should be a clippy lint for this! |
If clippy were to have suggested by_ref, i think i would have understood much more quickly. |
It could definitely change to use |
Change `while_let_on_iterator` suggestion to use `by_ref()` It came up in the discussion #7659 that suggesting `iter.by_ref()` is a clearer suggestion than `&mut iter`. I personally think they're equivalent, but if `by_ref()` is clearer to people then that should be the suggestion. changelog: Change `while_let_on_iterator` suggestion when using `&mut` to use `by_ref()`
I think this can be closed now. |
Lint name: while_let_on_iterator
I tried this code:
I expected to see this happen:
This should not trigger the lint because the iterator is used again after the loop. The purpose of explicitly calling
next()
is to make it clear that the iterator may not be fully consumed in the loop and the remaining items can be processed elsewhere.Instead, this happened: The lint triggers and tells me to use a
for
loop.Meta
Rust version (
rustc -Vv
):The text was updated successfully, but these errors were encountered: