Skip to content

Incorrect suggestion in while_let_on_iterator if iterator cannot be moved #8113

Closed

Description

@Alexendoo

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

Activity

  1. added
    C-bugCategory: Clippy is not doing the correct thing
    I-false-positiveIssue: The lint was triggered on code it shouldn't have
    on Dec 9, 2021
  2. added
    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
    on Dec 9, 2021
  3. WaffleLapkin commented on Dec 9, 2021

    @WaffleLapkin
    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

  4. 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[/+] on Dec 9, 2021
  5. Alexendoo commented on Dec 9, 2021

    @Alexendoo
    MemberAuthor

    @rustbot label -I-suggestion-causes-error

  6. removed
    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
    on Dec 9, 2021
  7. Alexendoo commented on Dec 9, 2021

    @Alexendoo
    MemberAuthor

    Whoops, wrong one 👀

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

  8. added
    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
    and removed
    I-false-positiveIssue: The lint was triggered on code it shouldn't have
    on Dec 9, 2021
  9. added 2 commits that reference this issue on Jan 4, 2022
    f2ea5fb
    92048f4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions