Skip to content

Commit 801e145

Browse files
committed
Addressed review
1 parent c78b85d commit 801e145

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

clippy_lints/src/approx_const.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ impl LateLintPass<'_> for ApproxConstant {
9292
impl ApproxConstant {
9393
fn check_known_consts(&self, cx: &LateContext<'_>, span: Span, s: symbol::Symbol, module: &str) {
9494
let s = s.as_str();
95-
if let Ok(s_value) = s.parse::<f64>() {
95+
if let Ok(maybe_constant) = s.parse::<f64>() {
9696
for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {
97-
if is_approx_const(constant, s, s_value, min_digits)
97+
if is_approx_const(constant, s, maybe_constant, min_digits)
9898
&& msrv.is_none_or(|msrv| self.msrv.meets(cx, msrv))
9999
{
100100
span_lint_and_help(
@@ -134,10 +134,14 @@ fn is_approx_const(constant: f64, value: &str, f_value: f64, min_digits: usize)
134134
true
135135
} else {
136136
// The value is a truncated constant
137+
138+
// Print constant with numeric formatting (`0`), with the length of `value` as minimum width
139+
// (`value_len$`), and with the same precision as `value` (`.value_prec$`).
140+
// See https://doc.rust-lang.org/std/fmt/index.html.
137141
let round_const = format!(
138-
"{constant:0value_len$.*}",
139-
count_digits_after_dot(value),
140-
value_len = value.len()
142+
"{constant:0value_len$.value_prec$}",
143+
value_len = value.len(),
144+
value_prec = count_digits_after_dot(value)
141145
);
142146
value == round_const
143147
}

0 commit comments

Comments
 (0)