Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Luro02 opened this issue Oct 17, 2022 · 1 comment · Fixed by #11049
Closed

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

Luro02 opened this issue Oct 17, 2022 · 1 comment · Fixed by #11049
Assignees
Labels
A-lint Area: New lints

Comments

@Luro02
Copy link

Luro02 commented Oct 17, 2022

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));
}
@Luro02 Luro02 added the A-lint Area: New lints label Oct 17, 2022
@Centri3
Copy link
Member

Centri3 commented Jun 26, 2023

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants