Skip to content

Commit 19d8a3e

Browse files
author
Michael Wright
committed
Improve approx_constant output
1 parent 4ee9ec9 commit 19d8a3e

File tree

3 files changed

+69
-29
lines changed

3 files changed

+69
-29
lines changed

clippy_lints/src/approx_const.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint;
1+
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::{meets_msrv, msrvs};
33
use rustc_ast::ast::{FloatTy, LitFloatType, LitKind};
44
use rustc_hir::{Expr, ExprKind};
@@ -88,15 +88,13 @@ impl ApproxConstant {
8888
if is_approx_const(constant, &s, min_digits)
8989
&& msrv.as_ref().map_or(true, |msrv| meets_msrv(self.msrv.as_ref(), msrv))
9090
{
91-
span_lint(
91+
span_lint_and_help(
9292
cx,
9393
APPROX_CONSTANT,
9494
e.span,
95-
&format!(
96-
"approximate value of `{}::consts::{}` found. \
97-
Consider using it directly",
98-
module, &name
99-
),
95+
&format!("approximate value of `{}::consts::{}` found", module, &name),
96+
None,
97+
"consider using the constant directly",
10098
);
10199
return;
102100
}

tests/ui/approx_const.stderr

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,171 @@
1-
error: approximate value of `f{32, 64}::consts::E` found. Consider using it directly
1+
error: approximate value of `f{32, 64}::consts::E` found
22
--> $DIR/approx_const.rs:4:16
33
|
44
LL | let my_e = 2.7182;
55
| ^^^^^^
66
|
77
= note: `-D clippy::approx-constant` implied by `-D warnings`
8+
= help: consider using the constant directly
89

9-
error: approximate value of `f{32, 64}::consts::E` found. Consider using it directly
10+
error: approximate value of `f{32, 64}::consts::E` found
1011
--> $DIR/approx_const.rs:5:20
1112
|
1213
LL | let almost_e = 2.718;
1314
| ^^^^^
15+
|
16+
= help: consider using the constant directly
1417

15-
error: approximate value of `f{32, 64}::consts::FRAC_1_PI` found. Consider using it directly
18+
error: approximate value of `f{32, 64}::consts::FRAC_1_PI` found
1619
--> $DIR/approx_const.rs:8:24
1720
|
1821
LL | let my_1_frac_pi = 0.3183;
1922
| ^^^^^^
23+
|
24+
= help: consider using the constant directly
2025

21-
error: approximate value of `f{32, 64}::consts::FRAC_1_SQRT_2` found. Consider using it directly
26+
error: approximate value of `f{32, 64}::consts::FRAC_1_SQRT_2` found
2227
--> $DIR/approx_const.rs:11:28
2328
|
2429
LL | let my_frac_1_sqrt_2 = 0.70710678;
2530
| ^^^^^^^^^^
31+
|
32+
= help: consider using the constant directly
2633

27-
error: approximate value of `f{32, 64}::consts::FRAC_1_SQRT_2` found. Consider using it directly
34+
error: approximate value of `f{32, 64}::consts::FRAC_1_SQRT_2` found
2835
--> $DIR/approx_const.rs:12:32
2936
|
3037
LL | let almost_frac_1_sqrt_2 = 0.70711;
3138
| ^^^^^^^
39+
|
40+
= help: consider using the constant directly
3241

33-
error: approximate value of `f{32, 64}::consts::FRAC_2_PI` found. Consider using it directly
42+
error: approximate value of `f{32, 64}::consts::FRAC_2_PI` found
3443
--> $DIR/approx_const.rs:15:24
3544
|
3645
LL | let my_frac_2_pi = 0.63661977;
3746
| ^^^^^^^^^^
47+
|
48+
= help: consider using the constant directly
3849

39-
error: approximate value of `f{32, 64}::consts::FRAC_2_SQRT_PI` found. Consider using it directly
50+
error: approximate value of `f{32, 64}::consts::FRAC_2_SQRT_PI` found
4051
--> $DIR/approx_const.rs:18:27
4152
|
4253
LL | let my_frac_2_sq_pi = 1.128379;
4354
| ^^^^^^^^
55+
|
56+
= help: consider using the constant directly
4457

45-
error: approximate value of `f{32, 64}::consts::FRAC_PI_2` found. Consider using it directly
58+
error: approximate value of `f{32, 64}::consts::FRAC_PI_2` found
4659
--> $DIR/approx_const.rs:21:24
4760
|
4861
LL | let my_frac_pi_2 = 1.57079632679;
4962
| ^^^^^^^^^^^^^
63+
|
64+
= help: consider using the constant directly
5065

51-
error: approximate value of `f{32, 64}::consts::FRAC_PI_3` found. Consider using it directly
66+
error: approximate value of `f{32, 64}::consts::FRAC_PI_3` found
5267
--> $DIR/approx_const.rs:24:24
5368
|
5469
LL | let my_frac_pi_3 = 1.04719755119;
5570
| ^^^^^^^^^^^^^
71+
|
72+
= help: consider using the constant directly
5673

57-
error: approximate value of `f{32, 64}::consts::FRAC_PI_4` found. Consider using it directly
74+
error: approximate value of `f{32, 64}::consts::FRAC_PI_4` found
5875
--> $DIR/approx_const.rs:27:24
5976
|
6077
LL | let my_frac_pi_4 = 0.785398163397;
6178
| ^^^^^^^^^^^^^^
79+
|
80+
= help: consider using the constant directly
6281

63-
error: approximate value of `f{32, 64}::consts::FRAC_PI_6` found. Consider using it directly
82+
error: approximate value of `f{32, 64}::consts::FRAC_PI_6` found
6483
--> $DIR/approx_const.rs:30:24
6584
|
6685
LL | let my_frac_pi_6 = 0.523598775598;
6786
| ^^^^^^^^^^^^^^
87+
|
88+
= help: consider using the constant directly
6889

69-
error: approximate value of `f{32, 64}::consts::FRAC_PI_8` found. Consider using it directly
90+
error: approximate value of `f{32, 64}::consts::FRAC_PI_8` found
7091
--> $DIR/approx_const.rs:33:24
7192
|
7293
LL | let my_frac_pi_8 = 0.3926990816987;
7394
| ^^^^^^^^^^^^^^^
95+
|
96+
= help: consider using the constant directly
7497

75-
error: approximate value of `f{32, 64}::consts::LN_10` found. Consider using it directly
98+
error: approximate value of `f{32, 64}::consts::LN_10` found
7699
--> $DIR/approx_const.rs:36:20
77100
|
78101
LL | let my_ln_10 = 2.302585092994046;
79102
| ^^^^^^^^^^^^^^^^^
103+
|
104+
= help: consider using the constant directly
80105

81-
error: approximate value of `f{32, 64}::consts::LN_2` found. Consider using it directly
106+
error: approximate value of `f{32, 64}::consts::LN_2` found
82107
--> $DIR/approx_const.rs:39:19
83108
|
84109
LL | let my_ln_2 = 0.6931471805599453;
85110
| ^^^^^^^^^^^^^^^^^^
111+
|
112+
= help: consider using the constant directly
86113

87-
error: approximate value of `f{32, 64}::consts::LOG10_E` found. Consider using it directly
114+
error: approximate value of `f{32, 64}::consts::LOG10_E` found
88115
--> $DIR/approx_const.rs:42:22
89116
|
90117
LL | let my_log10_e = 0.4342944819032518;
91118
| ^^^^^^^^^^^^^^^^^^
119+
|
120+
= help: consider using the constant directly
92121

93-
error: approximate value of `f{32, 64}::consts::LOG2_E` found. Consider using it directly
122+
error: approximate value of `f{32, 64}::consts::LOG2_E` found
94123
--> $DIR/approx_const.rs:45:21
95124
|
96125
LL | let my_log2_e = 1.4426950408889634;
97126
| ^^^^^^^^^^^^^^^^^^
127+
|
128+
= help: consider using the constant directly
98129

99-
error: approximate value of `f{32, 64}::consts::LOG2_10` found. Consider using it directly
130+
error: approximate value of `f{32, 64}::consts::LOG2_10` found
100131
--> $DIR/approx_const.rs:48:19
101132
|
102133
LL | let log2_10 = 3.321928094887362;
103134
| ^^^^^^^^^^^^^^^^^
135+
|
136+
= help: consider using the constant directly
104137

105-
error: approximate value of `f{32, 64}::consts::LOG10_2` found. Consider using it directly
138+
error: approximate value of `f{32, 64}::consts::LOG10_2` found
106139
--> $DIR/approx_const.rs:51:19
107140
|
108141
LL | let log10_2 = 0.301029995663981;
109142
| ^^^^^^^^^^^^^^^^^
143+
|
144+
= help: consider using the constant directly
110145

111-
error: approximate value of `f{32, 64}::consts::PI` found. Consider using it directly
146+
error: approximate value of `f{32, 64}::consts::PI` found
112147
--> $DIR/approx_const.rs:54:17
113148
|
114149
LL | let my_pi = 3.1415;
115150
| ^^^^^^
151+
|
152+
= help: consider using the constant directly
116153

117-
error: approximate value of `f{32, 64}::consts::PI` found. Consider using it directly
154+
error: approximate value of `f{32, 64}::consts::PI` found
118155
--> $DIR/approx_const.rs:55:21
119156
|
120157
LL | let almost_pi = 3.14;
121158
| ^^^^
159+
|
160+
= help: consider using the constant directly
122161

123-
error: approximate value of `f{32, 64}::consts::SQRT_2` found. Consider using it directly
162+
error: approximate value of `f{32, 64}::consts::SQRT_2` found
124163
--> $DIR/approx_const.rs:58:18
125164
|
126165
LL | let my_sq2 = 1.4142;
127166
| ^^^^^^
167+
|
168+
= help: consider using the constant directly
128169

129170
error: aborting due to 21 previous errors
130171

tests/ui/proc_macro.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
error: approximate value of `f{32, 64}::consts::PI` found. Consider using it directly
1+
error: approximate value of `f{32, 64}::consts::PI` found
22
--> $DIR/proc_macro.rs:10:14
33
|
44
LL | let _x = 3.14;
55
| ^^^^
66
|
77
= note: `#[deny(clippy::approx_constant)]` on by default
8+
= help: consider using the constant directly
89

910
error: aborting due to previous error
1011

0 commit comments

Comments
 (0)