Skip to content

if_not_else lints firing on else-ifs  #7892

@dswij

Description

@dswij

Lint name: if_not_else

I tried this code:

fn foo() {}
fn bar() {}
fn nothing() {}

#[warn(clippy::if_not_else)]
fn main() {
    let foo_disabled = true;
    let bar_disabled = true;
    
    if !foo_disabled {
        foo()
    } else if !bar_disabled {
        bar()
    } else {
        nothing()
    }
} 

I expected to see this happen: nothing

Instead, this happened: clippy suggests swapping the order of else-if and else block, which makes it less clear imo. There are scenarios where using not in else-ifs makes it more readable like the one above - you would expect it to be written in order of priority/preference. (try foo(), then try bar(), else do nothing)

warning: unnecessary boolean `not` operation
  --> src/main.rs:13:10
   |
13 |       else if !bar_disabled {
   |  __________^
14 | |         bar()
15 | |     } else {
16 | |         nothing()
17 | |     }
   | |_____^
   |
note: the lint level is defined here
  --> src/main.rs:5:8
   |
5  | #[warn(clippy::if_not_else)]
   |        ^^^^^^^^^^^^^^^^^^^
   = help: remove the `!` and swap the blocks of the `if`/`else`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_not_else

warning: `playground` (bin "playground") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 4.06s

Meta

Rust version (rustc -Vv):

rustc 1.46.0-nightly (f455e46ea 2020-06-20)
binary: rustc
commit-hash: f455e46eae1a227d735091091144601b467e1565
commit-date: 2020-06-20
host: x86_64-unknown-linux-gnu
release: 1.46.0-nightly
LLVM version: 10.0

See #7877 (comment)

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't havegood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions