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

float_cmp could catch float literals in match expressions #11555

Open
Centri3 opened this issue Sep 23, 2023 · 0 comments
Open

float_cmp could catch float literals in match expressions #11555

Centri3 opened this issue Sep 23, 2023 · 0 comments
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages

Comments

@Centri3
Copy link
Member

Centri3 commented Sep 23, 2023

Summary

rust-lang/rust#116098 removes the illegal_floating_point_literal_pattern lint. A float literal in a match expression is semantically equivalent to == and, if this is merged, I think it makes sense to suggest a guard.

If this lint is kept, this should be closed

Lint Name

float_cmp

Reproducer

I tried this code:

#![warn(clippy::float_cmp)]

fn main() {
    let x = 1.2331f64;
	// prints `a`
    match x {
        1.23f64 => println!("a"),
        _ => {},
    }
}

I expected to see this happen:

Clippy suggests:

#![warn(clippy::float_cmp)]

fn main() {
    let x = 1.2331f64;
	// prints nothing
    match x {
        x if (x - 1.23f64).abs() < f64::EPSILON => println!("a"),
        _ => {},
    }
}

Instead, this happened: No warning (from clippy)

Version

rustc 1.74.0-nightly (bdb0fa3ee 2023-09-19)
binary: rustc
commit-hash: bdb0fa3ee5ffb4cc1a1b53cbe8344a2b83b1ae2a
commit-date: 2023-09-19
host: x86_64-unknown-linux-gnu
release: 1.74.0-nightly
LLVM version: 17.0.0
@Centri3 Centri3 added the C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages label Sep 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages
Projects
None yet
Development

No branches or pull requests

1 participant