Skip to content

while_let_loop false positive #1693

Open
@crumblingstatue

Description

@crumblingstatue
fn main() {
    let mut lines = "foo".lines().peekable();
    // Gather remaining non-empty lines into a comment field.
    let mut comment = String::new();
    loop {
        if let Some(line) = lines.peek() {
            if line.is_empty() {
                break;
            } else {
                comment += line;
            }
        } else {
            break;
        }
        // Consume
        lines.next();
    }
}
warning: this loop could be written as a `while let` loop
  --> src/main.rs:5:5
   |
5  | /     loop {
6  | |         if let Some(line) = lines.peek() {
7  | |             if line.is_empty() {
8  | |                 break;
...  |
16 | |         lines.next();
17 | |     }
   | |_____^
   |
   = note: #[warn(while_let_loop)] on by default
help: try
   |     while let Some(line) = lines.peek() { .. }

This can't be trivially transformed into while let, since lines.next() is called unconditionally outside of the if let statement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-false-positiveIssue: The lint was triggered on code it shouldn't haveT-middleType: Probably requires verifiying types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions