Skip to content

while_let false positive with break checks before match statement #771

Closed
@ipetkov

Description

@ipetkov

Sample program:

#![feature(plugin)]
#![plugin(clippy)]

fn main() {
    let mut count = 100;
    loop {
        // Some check to avoid calling match statement
        if count > 10 {
            break;
        }

        match delete_the_database() {
            Some(_) => count = 0,
            None => break,
        }
    }
}

fn delete_the_database() -> Option<()> {
    Some(())
}

Suggestion given completely ignores the possibility of breaking before the match is even reached. This is a trivial example, but it is possible the earlier checks are imperative for breaking the loop before something irreversible is called further in the body.

src/main.rs:6:5: 16:6 warning: this loop could be written as a `while let` loop, #[warn(while_let_loop)] on by default
src/main.rs: 6     loop {
src/main.rs: 7         // Some check to avoid calling match statement
src/main.rs: 8         if count > 10 {
src/main.rs: 9             break;
src/main.rs:10         }
src/main.rs:11 
               ...
src/main.rs:6:5: 16:6 help: try
src/main.rs:       while let Some(_) = delete_the_database() { .. }

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions