rust-lang/rust-clippy

assert_eq! with a literal float should probably fire float_cmp_const not float_cmp

Open

#6,817 创建于 2021年3月1日

在 GitHub 查看
 (11 评论) (0 反应) (0 负责人)Rust (1,391 fork)batch import
C-bugI-false-positivegood first issue

仓库指标

Star
 (10,406 star)
PR 合并指标
 (平均合并 19天 22小时) (30 天内合并 113 个 PR)

描述

Example:

pub const RADIANS_PER_FURMAN: f64 = f64::consts::PI / 32_768_f64;
pub const FURMANS_PER_RADIAN: f64 = 32_768_f64 / f64::consts::PI;
pub const DEGREES_PER_FURMAN: f64 = 45_f64 / 8192_f64;
pub const FURMANS_PER_DEGREE: f64 = 8192_f64 / 45_f64;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn constants_are_right() {
        // decimal expansions retrieved with WolframAlpha and truncated by clippy
        assert_eq!(RADIANS_PER_FURMAN, 0.000_095_873_799_242_852_57); // exact
        assert_eq!(FURMANS_PER_RADIAN, 10_430.378_350_470_453); // exact
        assert_eq!(DEGREES_PER_FURMAN, 0.005_493_164_062_5); // exact
        assert_eq!(FURMANS_PER_DEGREE, 182.044_444_444_444_45); // exact
    }
}

Here float_cmp fires. Given that this is comparing a const to a literal float, however, this should almost certainly be float_cmp_const instead (which is in the restriction group, as opposed to float_cmp in pedantic). (float_cmp used to be more aggressively linted, but both lints are allow by default nowadays.)

This seems like a reasonable usage of exact floating point equality: defining consts with the intuitive definition and asserting that the produced value is precise within ±½ ULP, by checking against an alternatively produced value.

(Yes, furmans are a real unit, even if unusual/whimsical. Furman is the name for 1/65536th of a revolution, such that one revolution divides evenly into u16.)

贡献者指南