while_let_on_iterator
suggests broken fix when iterator is captured in FnMut
#7249
Labels
C-bug
Category: Clippy is not doing the correct thing
I-suggestion-causes-error
Issue: The suggestions provided by this Lint cause an ICE/error when applied
L-suggestion
Lint: Improving, adding or fixing lint suggestions
I have noticed a similar problem to #5844 and #6231, where Clippy suggests a for loop instead of a
while let
, causing an illegal move of the target iterator. In my case, it was because the iterator was borrowed by reference in anFnMut
closure.Minimal example:
Rust Playground
Clippy suggests changing the
while let Some(x) = it.next()
tofor x in it
, but we cannot move out of the mutable reference toit
. The correct suggestion is probablyfor x in &mut it
, similar to fixes in #6966, but it seems more difficult to detect aFnMut
without type information, which I'm not sure Clippy has access to. I believe the suggestionfor x in it
is better when the iterator has been moved into the closure.Meta
cargo clippy -V
:clippy 0.1.52 (88f19c6d 2021-05-03)
rustc -Vv
:The text was updated successfully, but these errors were encountered: