Skip to content

float_cmp could catch float literals in match expressions #11555

Open
@Centri3

Description

@Centri3

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions