Skip to content

needless_bool provides incorrect suggestions with if-else-if-else chains #4334

Closed
@phansch

Description

@phansch

Part of #3630

Given the following code:

#![warn(clippy::needless_bool)]

let returns_bool = || false;
let x = if b {
    true
} else if returns_bool() {
    false
} else {
    true
};

we currently provide a broken suggestion:

LL |       } else if returns_bool() {
   |  ____________^
LL | |         false
LL | |     } else {
LL | |         true
LL | |     };
   | |_____^ help: you can reduce it to: `!returns_bool()`

Applying the suggestion results in a syntax error:

let x = if b {
    true
} else !returns_bool();

The suggestion should be { !returns_bool() } - as it was before this PR: rust-lang/rust#61074

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