Skip to content

Commit bd9e81d

Browse files
authored
Add EULER_GAMMA and GOLDEN_RATIO to approx_constant (#17441)
The constants have been stabilized in Rust 1.94, so I figured why not add them? changelog: [`approx_constant`]: Add `EULER_GAMMA` and `GOLDEN_RATIO` constants
2 parents 4e15201 + b88f52c commit bd9e81d

4 files changed

Lines changed: 57 additions & 6 deletions

File tree

clippy_lints/src/approx_const.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ declare_clippy_lint! {
4242
impl_lint_pass!(ApproxConstant => [APPROX_CONSTANT]);
4343

4444
// Tuples are of the form (constant, name, min_digits, msrv)
45-
const KNOWN_CONSTS: [(f64, &str, usize, Option<RustcVersion>); 19] = [
45+
const KNOWN_CONSTS: [(f64, &str, usize, Option<RustcVersion>); 21] = [
4646
(f64::E, "E", 4, None),
4747
(f64::FRAC_1_PI, "FRAC_1_PI", 4, None),
4848
(f64::FRAC_1_SQRT_2, "FRAC_1_SQRT_2", 5, None),
@@ -62,6 +62,8 @@ const KNOWN_CONSTS: [(f64, &str, usize, Option<RustcVersion>); 19] = [
6262
(f64::PI, "PI", 3, None),
6363
(f64::SQRT_2, "SQRT_2", 5, None),
6464
(f64::TAU, "TAU", 3, Some(msrvs::TAU)),
65+
(f64::EULER_GAMMA, "EULER_GAMMA", 5, Some(msrvs::EULER_GAMMA)),
66+
(f64::GOLDEN_RATIO, "GOLDEN_RATIO", 5, Some(msrvs::GOLDEN_RATIO)),
6567
];
6668

6769
pub struct ApproxConstant {

clippy_utils/src/msrvs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ macro_rules! msrv_aliases {
2626
// names may refer to stabilized feature flags or library items
2727
msrv_aliases! {
2828
1,97,0 { ISOLATE_LOWEST_ONE, BIT_WIDTH }
29+
1,94,0 { EULER_GAMMA, GOLDEN_RATIO }
2930
1,93,0 { VEC_DEQUE_POP_BACK_IF, VEC_DEQUE_POP_FRONT_IF }
3031
1,91,0 { DURATION_FROM_MINUTES_HOURS }
3132
1,88,0 { LET_CHAINS, AS_CHUNKS }

tests/ui/approx_const.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ fn main() {
107107

108108
let no_tau = 6.3;
109109

110+
let my_euler_gamma = 0.577215664901533;
111+
//~^ approx_constant
112+
113+
let almost_euler_gamma = 0.5772;
114+
//~^ approx_constant
115+
116+
let no_euler_gamma = 0.577;
117+
118+
let my_golden_ration = 1.618033988749895;
119+
//~^ approx_constant
120+
121+
let almost_golden_ration = 1.6180;
122+
//~^ approx_constant
123+
124+
let no_almost_golden_ration = 1.618;
125+
110126
// issue #15194
111127
#[allow(clippy::excessive_precision)]
112128
let x: f64 = 3.1415926535897932384626433832;

tests/ui/approx_const.stderr

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,37 +184,69 @@ LL | let almost_tau = 6.28;
184184
|
185185
= help: consider using the constant directly
186186

187+
error: approximate value of `f{32, 64}::consts::EULER_GAMMA` found
188+
--> tests/ui/approx_const.rs:110:26
189+
|
190+
LL | let my_euler_gamma = 0.577215664901533;
191+
| ^^^^^^^^^^^^^^^^^
192+
|
193+
= help: consider using the constant directly
194+
195+
error: approximate value of `f{32, 64}::consts::EULER_GAMMA` found
196+
--> tests/ui/approx_const.rs:113:30
197+
|
198+
LL | let almost_euler_gamma = 0.5772;
199+
| ^^^^^^
200+
|
201+
= help: consider using the constant directly
202+
203+
error: approximate value of `f{32, 64}::consts::GOLDEN_RATIO` found
204+
--> tests/ui/approx_const.rs:118:28
205+
|
206+
LL | let my_golden_ration = 1.618033988749895;
207+
| ^^^^^^^^^^^^^^^^^
208+
|
209+
= help: consider using the constant directly
210+
211+
error: approximate value of `f{32, 64}::consts::GOLDEN_RATIO` found
212+
--> tests/ui/approx_const.rs:121:32
213+
|
214+
LL | let almost_golden_ration = 1.6180;
215+
| ^^^^^^
216+
|
217+
= help: consider using the constant directly
218+
187219
error: approximate value of `f{32, 64}::consts::PI` found
188-
--> tests/ui/approx_const.rs:112:18
220+
--> tests/ui/approx_const.rs:128:18
189221
|
190222
LL | let x: f64 = 3.1415926535897932384626433832;
191223
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
192224
|
193225
= help: consider using the constant directly
194226

195227
error: approximate value of `f{32, 64}::consts::PI` found
196-
--> tests/ui/approx_const.rs:116:18
228+
--> tests/ui/approx_const.rs:132:18
197229
|
198230
LL | let _: f64 = 003.14159265358979311599796346854418516159057617187500;
199231
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
200232
|
201233
= help: consider using the constant directly
202234

203235
error: approximate value of `f{32, 64}::consts::FRAC_1_SQRT_2` found
204-
--> tests/ui/approx_const.rs:119:32
236+
--> tests/ui/approx_const.rs:135:32
205237
|
206238
LL | let almost_frac_1_sqrt_2 = 00.70711;
207239
| ^^^^^^^^
208240
|
209241
= help: consider using the constant directly
210242

211243
error: approximate value of `f{32, 64}::consts::FRAC_1_SQRT_2` found
212-
--> tests/ui/approx_const.rs:122:32
244+
--> tests/ui/approx_const.rs:138:32
213245
|
214246
LL | let almost_frac_1_sqrt_2 = 00.707_11;
215247
| ^^^^^^^^^
216248
|
217249
= help: consider using the constant directly
218250

219-
error: aborting due to 27 previous errors
251+
error: aborting due to 31 previous errors
220252

0 commit comments

Comments
 (0)