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

while_let_on_iterator causes compile error #5844

Closed
JarredAllen opened this issue Jul 25, 2020 · 2 comments · Fixed by #6966
Closed

while_let_on_iterator causes compile error #5844

JarredAllen opened this issue Jul 25, 2020 · 2 comments · Fixed by #6966
Labels
C-bug Category: Clippy is not doing the correct thing C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages I-false-positive Issue: The lint was triggered on code it shouldn't have 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 T-MIR Type: This lint will require working with the MIR

Comments

@JarredAllen
Copy link
Contributor

With this code:

let nums = vec![1, 2, 3, 4, 5, 1, 2, 3, 4, 5];
let mut iter = nums.iter().peekable();
while iter.peek().is_some() {
    while let Some(&next_elem) = iter.next() {
        if next_elem == 2 {
            break;
        }
    }
}

I expected to see this happen: No suggestion or a suggestion which doesn't cause a compile error

Instead, this happened: Clippy makes this suggestion which causes a compile error:

let nums = vec![1, 2, 3, 4, 5, 1, 2, 3, 4, 5];
let mut iter = nums.iter().peekable();
while iter.peek().is_some() {
    for &next_elem in iter {
        if next_elem == 2 {
            break;
        }
    }
}

which leads to the following compilation error

error[E0382]: borrow of moved value: `iter`
  --> src/main.rs:16:11
   |
15 |     let mut iter = nums.iter().peekable();
   |         -------- move occurs because `iter` has type `std::iter::Peekable<std::slice::Iter<'_, i32>>`, which does not implement the `Copy` trait
16 |     while iter.peek().is_some() {
   |           ^^^^ value borrowed here after move
17 |         for &next_elem in iter {
   |                           ----
   |                           |
   |                           value moved here, in previous iteration of loop
   |                           help: consider borrowing to avoid moving into the for loop: `&iter`

error: aborting due to previous err

Following that suggestion by borrowing there doesn't fix the issue, as it then suggests you remove the borrow due to &iter not being an iterable.

However, borrowing mutably (adding &mut) results in code which compiles and passes clippy. It would be nice if clippy could have this &mut in the original suggestion (though I don't know enough about the borrow checker to know how easy that would be to implement).

Here it is on the playground

Meta

  • cargo clippy -V: clippy 0.0.212 (5c1f21c 2020-07-13)
  • rustc -Vv:

rustc 1.45.0 (5c1f21c3b 2020-07-13)
binary: rustc
commit-hash: 5c1f21c3b82297671ad3ae1e8c942d2ca92e84f2
commit-date: 2020-07-13
host: x86_64-unknown-linux-gnu
release: 1.45.0
LLVM version: 10.0

@JarredAllen JarredAllen added the C-bug Category: Clippy is not doing the correct thing label Jul 25, 2020
@flip1995 flip1995 added 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 C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels Jul 29, 2020
@matthiaskrgr matthiaskrgr added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 28, 2020
@matthiaskrgr
Copy link
Member

Issue still persists.
Smaller example:

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

@ThibsG
Copy link
Contributor

ThibsG commented Jan 5, 2021

@rustbot label +T-MIR

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 C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages I-false-positive Issue: The lint was triggered on code it shouldn't have 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 T-MIR Type: This lint will require working with the MIR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants