Skip to content

Commit 6f62e68

Browse files
committed
Auto merge of rust-lang#136641 - matthiaskrgr:rollup-lajwje5, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#136073 (Always compute coroutine layout for eagerly emitting recursive layout errors) - rust-lang#136235 (Pretty print pattern type values with transmute if they don't satisfy their pattern) - rust-lang#136311 (Ensure that we never try to monomorphize the upcasting or vtable calls of impossible dyn types) - rust-lang#136315 (Use short ty string for binop and unop errors) - rust-lang#136393 (Fix accidentally not emitting overflowing literals lints anymore in patterns) - rust-lang#136435 (Simplify some code for lowering THIR patterns) - rust-lang#136630 (Change two std process tests to not output to std{out,err}, and fix test suite stat reset in bootstrap CI test rendering) r? `@ghost` `@rustbot` modify labels: rollup try-job: aarch64-gnu-debug
2 parents d2497ac + ece0658 commit 6f62e68

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

clippy_lints/src/approx_const.rs

+17-21
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use clippy_utils::diagnostics::span_lint_and_help;
33
use clippy_utils::msrvs::{self, Msrv};
44
use rustc_ast::ast::{FloatTy, LitFloatType, LitKind};
55
use rustc_attr_parsing::RustcVersion;
6-
use rustc_hir::{Expr, ExprKind};
6+
use rustc_hir::{HirId, Lit};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::impl_lint_pass;
9-
use rustc_span::symbol;
9+
use rustc_span::{Span, symbol};
1010
use std::f64::consts as f64;
1111

1212
declare_clippy_lint! {
@@ -73,30 +73,36 @@ impl ApproxConstant {
7373
msrv: conf.msrv.clone(),
7474
}
7575
}
76+
}
7677

77-
fn check_lit(&self, cx: &LateContext<'_>, lit: &LitKind, e: &Expr<'_>) {
78-
match *lit {
78+
impl<'tcx> LateLintPass<'tcx> for ApproxConstant {
79+
fn check_lit(&mut self, cx: &LateContext<'_>, _hir_id: HirId, lit: &Lit, _negated: bool) {
80+
match lit.node {
7981
LitKind::Float(s, LitFloatType::Suffixed(fty)) => match fty {
80-
FloatTy::F16 => self.check_known_consts(cx, e, s, "f16"),
81-
FloatTy::F32 => self.check_known_consts(cx, e, s, "f32"),
82-
FloatTy::F64 => self.check_known_consts(cx, e, s, "f64"),
83-
FloatTy::F128 => self.check_known_consts(cx, e, s, "f128"),
82+
FloatTy::F16 => self.check_known_consts(cx, lit.span, s, "f16"),
83+
FloatTy::F32 => self.check_known_consts(cx, lit.span, s, "f32"),
84+
FloatTy::F64 => self.check_known_consts(cx, lit.span, s, "f64"),
85+
FloatTy::F128 => self.check_known_consts(cx, lit.span, s, "f128"),
8486
},
8587
// FIXME(f16_f128): add `f16` and `f128` when these types become stable.
86-
LitKind::Float(s, LitFloatType::Unsuffixed) => self.check_known_consts(cx, e, s, "f{32, 64}"),
88+
LitKind::Float(s, LitFloatType::Unsuffixed) => self.check_known_consts(cx, lit.span, s, "f{32, 64}"),
8789
_ => (),
8890
}
8991
}
9092

91-
fn check_known_consts(&self, cx: &LateContext<'_>, e: &Expr<'_>, s: symbol::Symbol, module: &str) {
93+
extract_msrv_attr!(LateContext);
94+
}
95+
96+
impl ApproxConstant {
97+
fn check_known_consts(&self, cx: &LateContext<'_>, span: Span, s: symbol::Symbol, module: &str) {
9298
let s = s.as_str();
9399
if s.parse::<f64>().is_ok() {
94100
for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {
95101
if is_approx_const(constant, s, min_digits) && msrv.is_none_or(|msrv| self.msrv.meets(msrv)) {
96102
span_lint_and_help(
97103
cx,
98104
APPROX_CONSTANT,
99-
e.span,
105+
span,
100106
format!("approximate value of `{module}::consts::{name}` found"),
101107
None,
102108
"consider using the constant directly",
@@ -110,16 +116,6 @@ impl ApproxConstant {
110116

111117
impl_lint_pass!(ApproxConstant => [APPROX_CONSTANT]);
112118

113-
impl<'tcx> LateLintPass<'tcx> for ApproxConstant {
114-
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
115-
if let ExprKind::Lit(lit) = &e.kind {
116-
self.check_lit(cx, &lit.node, e);
117-
}
118-
}
119-
120-
extract_msrv_attr!(LateContext);
121-
}
122-
123119
/// Returns `false` if the number of significant figures in `value` are
124120
/// less than `min_digits`; otherwise, returns true if `value` is equal
125121
/// to `constant`, rounded to the number of digits present in `value`.

0 commit comments

Comments
 (0)