Uniformly handle HIR literals in visitors and lints · rust-lang/rust@9a2073d (original) (raw)
`@@ -3,10 +3,10 @@ use clippy_utils::diagnostics::span_lint_and_help;
`
3
3
`use clippy_utils::msrvs::{self, Msrv};
`
4
4
`use rustc_ast::ast::{FloatTy, LitFloatType, LitKind};
`
5
5
`use rustc_attr_parsing::RustcVersion;
`
6
``
`-
use rustc_hir::{Expr, ExprKind};
`
``
6
`+
use rustc_hir::{HirId, Lit};
`
7
7
`use rustc_lint::{LateContext, LateLintPass};
`
8
8
`use rustc_session::impl_lint_pass;
`
9
``
`-
use rustc_span::symbol;
`
``
9
`+
use rustc_span::{Span, symbol};
`
10
10
`use std::f64::consts as f64;
`
11
11
``
12
12
`declare_clippy_lint! {
`
`@@ -73,30 +73,36 @@ impl ApproxConstant {
`
73
73
`msrv: conf.msrv.clone(),
`
74
74
`}
`
75
75
`}
`
``
76
`+
}
`
76
77
``
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 {
`
79
81
`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"),
`
84
86
`},
`
85
87
`` // 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}"),
`
87
89
` _ => (),
`
88
90
`}
`
89
91
`}
`
90
92
``
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) {
`
92
98
`let s = s.as_str();
`
93
99
`if s.parse::().is_ok() {
`
94
100
`for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {
`
95
101
`if is_approx_const(constant, s, min_digits) && msrv.is_none_or(|msrv| self.msrv.meets(msrv)) {
`
96
102
`span_lint_and_help(
`
97
103
` cx,
`
98
104
`APPROX_CONSTANT,
`
99
``
`-
e.span,
`
``
105
`+
span,
`
100
106
`` format!("approximate value of {module}::consts::{name}
found"),
``
101
107
`None,
`
102
108
`"consider using the constant directly",
`
`@@ -110,16 +116,6 @@ impl ApproxConstant {
`
110
116
``
111
117
`impl_lint_pass!(ApproxConstant => [APPROX_CONSTANT]);
`
112
118
``
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
``
-
123
119
`` /// Returns false
if the number of significant figures in value
are
``
124
120
`` /// less than min_digits
; otherwise, returns true if value
is equal
``
125
121
`` /// to constant
, rounded to the number of digits present in value
.
``