rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.24k stars 1.51k forks source link

Spot useless nested if test conditions #1598

Open leonardo-m opened 7 years ago

leonardo-m commented 7 years ago

I'd like Clippy to spot situations like this, here the second "if" condition is useless:

fn foo7(x: u32) {
    if x == 5 {
        println!("A");
        if x == 5 {
            println!("B");
        }
    }
}
mcarton commented 7 years ago

Most of what you propose look a lot like abstract interpretation with octagonal domains. We don't do that yet, but it would be an interesting project.

matthiaskrgr commented 3 years ago

For a more advanced version of this, clippy should also notice thatif x.is_some() and if let Some(y) = x are basically the same

fn foo(o: Option<String>) {
    if o.is_some() {
        // some code
        if let Some(s) = o {  // same as previous cond
            // some more code
        }
    }
}