Skip to content

Lint to replace assignments in both if/then branches with a single one #15171

Open
@leonardo-m

Description

@leonardo-m

What it does

Suggests to replace two assignments in if/then with one.
This too comes from code I've found in the wild, that here I've reduced.

Advantage

DRYer code. Possibly a bit shorter code.

Drawbacks

If done well I think there are no drawbacks.

Example

#![warn(clippy::all)]
#![warn(clippy::nursery)]
#![warn(clippy::pedantic)]

pub const fn foo1(a: &mut u32, b: bool) {
    if b {
        *a = 1;
    } else {
        *a = 2;
    }
}

fn main() {}

Could be written as:

pub const fn foo2(a: &mut u32, b: bool) {
    *a = if b { 1 } else { 2 };
}

Metadata

Metadata

Assignees

No one assigned

    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