Skip to content

False positive while_let_on_iterator #2926

Closed
@dwijnand

Description

@dwijnand

Given

let args: Vec<String> = std::env::args().collect();
let mut args = args.iter().skip(1); // skip the path of the executable
while let Some(_arg) = args.next() {
    let blank = &String::new();
    let _next = || { args.next().unwrap_or(blank) };
}

Clippy warns:

warning: this loop could be written as a `for` loop                      ] 0/1: t
 --> src/lib.rs:4:28
  |
4 |     while let Some(_arg) = args.next() {
  |                            ^^^^^^^^^^^ help: try: `for _arg in args { .. }`
  |
  = note: #[warn(while_let_on_iterator)] on by default
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#while_let_on_iterator

However that results in:

error[E0382]: capture of moved value: `args`                             ] 0/1: t
 --> src/lib.rs:6:21
  |
4 |     for _arg in args {
  |                 ---- value moved here
5 |         let blank = &String::new();
6 |         let _next = || { args.next().unwrap_or(blank) };
  |                     ^^ value captured here after move
  |
  = note: move occurs because `args` has type `std::iter::Skip<std::slice::Iter<'_, std::string::String>>`, which does not implement the `Copy` trait

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 appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions