Skip to content
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

Incorrect suggestion in while_let_on_iterator if iterator cannot be moved #8113

Closed
Alexendoo opened this issue Dec 9, 2021 · 3 comments · Fixed by #8221
Closed

Incorrect suggestion in while_let_on_iterator if iterator cannot be moved #8113

Alexendoo opened this issue Dec 9, 2021 · 3 comments · Fixed by #8221
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

Comments

@Alexendoo
Copy link
Member

Summary

Same as #7846, but for while_let_on_iterator

Lint Name

while_let_on_iterator

Reproducer

I tried this code:

fn main() {
    let mut arr = [vec![1, 2, 3].into_iter()];
    while let Some(x) = arr[0].next() {}
}

I saw this happen:

warning: this loop could be written as a `for` loop
 --> src\main.rs:3:5
  |
3 |     while let Some(x) = arr[0].next() {}
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in arr[0]`
  |
  = note: `#[warn(clippy::while_let_on_iterator)]` on by default

the suggestion causes the error

error[E0508]: cannot move out of type `[std::vec::IntoIter<i32>; 1]`, a non-copy array
 --> src\main.rs:3:14
  |
3 |     for x in arr[0] {}
  |              ^^^^^^
  |              |
  |              cannot move out of here
  |              move occurs because `arr[_]` has type `std::vec::IntoIter<i32>`, which does not implement the `Copy` trait

I expected to see this happen:

No lint emitted

Version

rustc 1.59.0-nightly (e6b883c74 2021-12-08)
binary: rustc
commit-hash: e6b883c74f49f32cb5d1cbad3457f2b8805a4a38
commit-date: 2021-12-08
host: x86_64-pc-windows-msvc
release: 1.59.0-nightly
LLVM version: 13.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

@Alexendoo Alexendoo added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 9, 2021
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 9, 2021
@WaffleLapkin
Copy link
Member

I don't think it's false positive, as there can be correct suggestion:

for x in &mut arr[0] {}
// or
for x in arr[0].by_ref() {}

i.e. if iterator can't be moved we should suggest using unique references to the said iterator

@Alexendoo Alexendoo changed the title False positive in while_let_on_iterator if iterator cannot be moved Incorrect suggestion in while_let_on_iterator if iterator cannot be moved Dec 9, 2021
@Alexendoo
Copy link
Member Author

@rustbot label -I-suggestion-causes-error

@rustbot rustbot removed the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 9, 2021
@Alexendoo
Copy link
Member Author

Whoops, wrong one 👀

@rustbot label +I-suggestion-causes-error -I-false-positive

@rustbot rustbot added I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied and removed I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 9, 2021
bors added a commit that referenced this issue Jan 4, 2022
Better detect when a field can be moved from in `while_let_on_iterator`

fixes #8113

changelog: Better detect when a field can be moved from in `while_let_on_iterator`
@bors bors closed this as completed in 92048f4 Jan 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants