Skip to content

Suggest {f32, f64}::is_finite and {f32, f64}::is_infinite #9665

Closed
@Luro02

Description

@Luro02

What it does

Detects manual implementations of these functions and suggests replacing them with a call to that function.

Lint Name

float_manual_finite/float_manual_infinite

Category

style, pedantic

Advantage

Drawbacks

  • maybe changes the meaning of the code?

Example

fn double(float: f32) -> f32 {
    if float == f32::INFINITY || float == f32::NEG_INFINITY {
        println!("is infinite");
    }
    
    float * 2.0
}


fn main() {
    dbg!(double(6.63E-34));
}

Could be written as:

fn double(float: f32) -> f32 {
    if float.is_infinite() {
        println!("is infinite");
    }
    
    float * 2.0
}


fn main() {
    dbg!(double(6.63E-34));
}

fn double(float: f32) -> f32 {
    assert!(float != f32::INFINITY && float != f32::NEG_INFINITY);

    float * 2.0
}


fn main() {
    dbg!(double(6.63E-34));
}

Could be written as:

fn double(float: f32) -> f32 {
    assert!(float.is_finite());

    float * 2.0
}


fn main() {
    dbg!(double(6.63E-34));
}

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