Skip to content

Suggest saturating_sub for if i != 0 { i -= 1; } #5399

Closed
@Luro02

Description

@Luro02
#![warn(clippy::all)]

fn main() {
    let end = 10;
    let start = 5;
    
    let mut i = end - start;
    
    if i != 0 {
        i -= 1;
    }
    
    println!("i: {}", i);
}

could be optimized to

#![warn(clippy::all)]

fn main() {
    let end = 10;
    let start = 5;
    
    let mut i = (end - start).saturating_sub(1);

    println!("i: {}", i);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsL-styleLint: Belongs in the style lint groupL-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