Skip to content

New lint: prefer bool::then #6760

Closed
@magurotuna

Description

@magurotuna

What it does

Checks for if-else that could be rewritten to bool::then

Categories (optional)

  • Kind: style restriction

Reduces redundancy and lines of code by leveraging a new stable API bool::then

Drawbacks

None.

Example

let a = if foo {
    println!("hello foo!");
    Some(bar())
} else {
    None
};

Could be written as:

let a = foo.then(|| {
    println!("hello foo!");
    bar()
});

Note that the following cannot be rewritten using bool::then because it has some action in else block:

let a = if foo {
    println!("hello foo!");
    Some(bar())
} else {
    println!("else!");
    None
};

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions