approx_constant
code:text
What it does
Checks for floating point literals that approximate constants which are defined in std::f32::consts or std::f64::consts, respectively, suggesting to use the predefined constant.
Why is this bad?
Usually, the definition in the standard library is more precise than what people come up with. If you find that your definition is actually more precise, please file a Rust issue.
Example
let x = 3.14;
let y = 1_f64 / x;
Use predefined constants instead:
let x = std::f32::consts::PI;
let y = std::f64::consts::FRAC_1_PI;
Configuration
This lint has the following configuration variables:
msrv: Option<String>: The minimum rust version that the project supports (defaults to None)
メモ
ここ で定義されている定数を見てる、実際のロジックはここ。 PI(円周率)を例に軽く説明する。
code:rust
// Tuples are of the form (constant, name, min_digits, msrv)
// -snip-
(f64::PI, "PI", 3, None),
// -snip-
];
f64::PI の値は ここ で定義されていて、タプルの第三引数の文字数より長く一致していたらClippyが怒る。 つまり、 3.14 , 3.141, 3.1415...じゃ怒られて 3.1 , 3.13, 3.142... はClippyがPIの近似値だと判断しないので怒らない。f64::PIの文字列と実際に与えられたリテラルの文字列を比較している。
ただしMSRVもあって、log(10)2 とかはv.1.43.0 以降じゃないとサポートされないので注意されたい。