Skip to content

Commit e5ae3f5

Browse files
committed
Auto merge of rust-lang#7629 - mikerite:fix-7623-2, r=xFrednet
Make `approx_const` MSRV aware changelog: [`approx_const`]: Add MRSV checks for LOG10_2 and LOG2_10. Fixes rust-lang#7623
2 parents a8c269a + 19d8a3e commit e5ae3f5

File tree

8 files changed

+145
-82
lines changed

8 files changed

+145
-82
lines changed

clippy_lints/src/approx_const.rs

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use clippy_utils::diagnostics::span_lint;
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::{meets_msrv, msrvs};
23
use rustc_ast::ast::{FloatTy, LitFloatType, LitKind};
34
use rustc_hir::{Expr, ExprKind};
4-
use rustc_lint::{LateContext, LateLintPass};
5-
use rustc_session::{declare_lint_pass, declare_tool_lint};
5+
use rustc_lint::{LateContext, LateLintPass, LintContext};
6+
use rustc_semver::RustcVersion;
7+
use rustc_session::{declare_tool_lint, impl_lint_pass};
68
use rustc_span::symbol;
79
use std::f64::consts as f64;
810

@@ -36,68 +38,81 @@ declare_clippy_lint! {
3638
"the approximate of a known float constant (in `std::fXX::consts`)"
3739
}
3840

39-
// Tuples are of the form (constant, name, min_digits)
40-
const KNOWN_CONSTS: [(f64, &str, usize); 18] = [
41-
(f64::E, "E", 4),
42-
(f64::FRAC_1_PI, "FRAC_1_PI", 4),
43-
(f64::FRAC_1_SQRT_2, "FRAC_1_SQRT_2", 5),
44-
(f64::FRAC_2_PI, "FRAC_2_PI", 5),
45-
(f64::FRAC_2_SQRT_PI, "FRAC_2_SQRT_PI", 5),
46-
(f64::FRAC_PI_2, "FRAC_PI_2", 5),
47-
(f64::FRAC_PI_3, "FRAC_PI_3", 5),
48-
(f64::FRAC_PI_4, "FRAC_PI_4", 5),
49-
(f64::FRAC_PI_6, "FRAC_PI_6", 5),
50-
(f64::FRAC_PI_8, "FRAC_PI_8", 5),
51-
(f64::LN_10, "LN_10", 5),
52-
(f64::LN_2, "LN_2", 5),
53-
(f64::LOG10_E, "LOG10_E", 5),
54-
(f64::LOG2_E, "LOG2_E", 5),
55-
(f64::LOG2_10, "LOG2_10", 5),
56-
(f64::LOG10_2, "LOG10_2", 5),
57-
(f64::PI, "PI", 3),
58-
(f64::SQRT_2, "SQRT_2", 5),
41+
// Tuples are of the form (constant, name, min_digits, msrv)
42+
const KNOWN_CONSTS: [(f64, &str, usize, Option<RustcVersion>); 18] = [
43+
(f64::E, "E", 4, None),
44+
(f64::FRAC_1_PI, "FRAC_1_PI", 4, None),
45+
(f64::FRAC_1_SQRT_2, "FRAC_1_SQRT_2", 5, None),
46+
(f64::FRAC_2_PI, "FRAC_2_PI", 5, None),
47+
(f64::FRAC_2_SQRT_PI, "FRAC_2_SQRT_PI", 5, None),
48+
(f64::FRAC_PI_2, "FRAC_PI_2", 5, None),
49+
(f64::FRAC_PI_3, "FRAC_PI_3", 5, None),
50+
(f64::FRAC_PI_4, "FRAC_PI_4", 5, None),
51+
(f64::FRAC_PI_6, "FRAC_PI_6", 5, None),
52+
(f64::FRAC_PI_8, "FRAC_PI_8", 5, None),
53+
(f64::LN_2, "LN_2", 5, None),
54+
(f64::LN_10, "LN_10", 5, None),
55+
(f64::LOG2_10, "LOG2_10", 5, Some(msrvs::LOG2_10)),
56+
(f64::LOG2_E, "LOG2_E", 5, None),
57+
(f64::LOG10_2, "LOG10_2", 5, Some(msrvs::LOG10_2)),
58+
(f64::LOG10_E, "LOG10_E", 5, None),
59+
(f64::PI, "PI", 3, None),
60+
(f64::SQRT_2, "SQRT_2", 5, None),
5961
];
6062

61-
declare_lint_pass!(ApproxConstant => [APPROX_CONSTANT]);
63+
pub struct ApproxConstant {
64+
msrv: Option<RustcVersion>,
65+
}
6266

63-
impl<'tcx> LateLintPass<'tcx> for ApproxConstant {
64-
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
65-
if let ExprKind::Lit(lit) = &e.kind {
66-
check_lit(cx, &lit.node, e);
67+
impl ApproxConstant {
68+
#[must_use]
69+
pub fn new(msrv: Option<RustcVersion>) -> Self {
70+
Self { msrv }
71+
}
72+
73+
fn check_lit(&self, cx: &LateContext<'_>, lit: &LitKind, e: &Expr<'_>) {
74+
match *lit {
75+
LitKind::Float(s, LitFloatType::Suffixed(fty)) => match fty {
76+
FloatTy::F32 => self.check_known_consts(cx, e, s, "f32"),
77+
FloatTy::F64 => self.check_known_consts(cx, e, s, "f64"),
78+
},
79+
LitKind::Float(s, LitFloatType::Unsuffixed) => self.check_known_consts(cx, e, s, "f{32, 64}"),
80+
_ => (),
6781
}
6882
}
69-
}
7083

71-
fn check_lit(cx: &LateContext<'_>, lit: &LitKind, e: &Expr<'_>) {
72-
match *lit {
73-
LitKind::Float(s, LitFloatType::Suffixed(fty)) => match fty {
74-
FloatTy::F32 => check_known_consts(cx, e, s, "f32"),
75-
FloatTy::F64 => check_known_consts(cx, e, s, "f64"),
76-
},
77-
LitKind::Float(s, LitFloatType::Unsuffixed) => check_known_consts(cx, e, s, "f{32, 64}"),
78-
_ => (),
84+
fn check_known_consts(&self, cx: &LateContext<'_>, e: &Expr<'_>, s: symbol::Symbol, module: &str) {
85+
let s = s.as_str();
86+
if s.parse::<f64>().is_ok() {
87+
for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {
88+
if is_approx_const(constant, &s, min_digits)
89+
&& msrv.as_ref().map_or(true, |msrv| meets_msrv(self.msrv.as_ref(), msrv))
90+
{
91+
span_lint_and_help(
92+
cx,
93+
APPROX_CONSTANT,
94+
e.span,
95+
&format!("approximate value of `{}::consts::{}` found", module, &name),
96+
None,
97+
"consider using the constant directly",
98+
);
99+
return;
100+
}
101+
}
102+
}
79103
}
80104
}
81105

82-
fn check_known_consts(cx: &LateContext<'_>, e: &Expr<'_>, s: symbol::Symbol, module: &str) {
83-
let s = s.as_str();
84-
if s.parse::<f64>().is_ok() {
85-
for &(constant, name, min_digits) in &KNOWN_CONSTS {
86-
if is_approx_const(constant, &s, min_digits) {
87-
span_lint(
88-
cx,
89-
APPROX_CONSTANT,
90-
e.span,
91-
&format!(
92-
"approximate value of `{}::consts::{}` found. \
93-
Consider using it directly",
94-
module, &name
95-
),
96-
);
97-
return;
98-
}
106+
impl_lint_pass!(ApproxConstant => [APPROX_CONSTANT]);
107+
108+
impl<'tcx> LateLintPass<'tcx> for ApproxConstant {
109+
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
110+
if let ExprKind::Lit(lit) = &e.kind {
111+
self.check_lit(cx, &lit.node, e);
99112
}
100113
}
114+
115+
extract_msrv_attr!(LateContext);
101116
}
102117

103118
/// Returns `false` if the number of significant figures in `value` are

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
18691869
store.register_late_pass(|| Box::new(needless_bool::NeedlessBool));
18701870
store.register_late_pass(|| Box::new(needless_bool::BoolComparison));
18711871
store.register_late_pass(|| Box::new(needless_for_each::NeedlessForEach));
1872-
store.register_late_pass(|| Box::new(approx_const::ApproxConstant));
18731872
store.register_late_pass(|| Box::new(misc::MiscLints));
18741873
store.register_late_pass(|| Box::new(eta_reduction::EtaReduction));
18751874
store.register_late_pass(|| Box::new(identity_op::IdentityOp));
@@ -1898,6 +1897,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
18981897
});
18991898

19001899
let avoid_breaking_exported_api = conf.avoid_breaking_exported_api;
1900+
store.register_late_pass(move || Box::new(approx_const::ApproxConstant::new(msrv)));
19011901
store.register_late_pass(move || Box::new(methods::Methods::new(avoid_breaking_exported_api, msrv)));
19021902
store.register_late_pass(move || Box::new(matches::Matches::new(msrv)));
19031903
store.register_early_pass(move || Box::new(manual_non_exhaustive::ManualNonExhaustive::new(msrv)));

clippy_lints/src/utils/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ define_Conf! {
132132
///
133133
/// Suppress lints whenever the suggested change would cause breakage for other crates.
134134
(avoid_breaking_exported_api: bool = true),
135-
/// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, REDUNDANT_FIELD_NAMES, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE.
135+
/// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, REDUNDANT_FIELD_NAMES, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE, APPROX_CONSTANT.
136136
///
137137
/// The minimum rust version that the project supports
138138
(msrv: Option<String> = None),

clippy_utils/src/msrvs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ msrv_aliases! {
1717
1,50,0 { BOOL_THEN }
1818
1,46,0 { CONST_IF_MATCH }
1919
1,45,0 { STR_STRIP_PREFIX }
20+
1,43,0 { LOG2_10, LOG10_2 }
2021
1,42,0 { MATCHES_MACRO }
2122
1,41,0 { RE_REBALANCING_COHERENCE, RESULT_MAP_OR_ELSE }
2223
1,40,0 { MEM_TAKE, NON_EXHAUSTIVE, OPTION_AS_DEREF }

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/min_rust_version_attr.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use std::ops::{Deref, RangeFrom};
66

7+
fn approx_const() {
8+
let log2_10 = 3.321928094887362;
9+
let log10_2 = 0.301029995663981;
10+
}
11+
712
fn cloned_instead_of_copied() {
813
let _ = [1].iter().cloned();
914
}

0 commit comments

Comments
 (0)