Skip to content

Commit 37ddd9f

Browse files
samueltardieuComputerDruid
authored andcommitted
refactor legacy_numeric_constants to check calls instead of paths
This also moves the lint to be posted on the call.
1 parent 88ee494 commit 37ddd9f

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

clippy_lints/src/legacy_numeric_constants.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_then;
3+
use clippy_utils::is_from_proc_macro;
34
use clippy_utils::msrvs::{self, Msrv};
4-
use clippy_utils::{get_parent_expr, is_from_proc_macro};
55
use hir::def_id::DefId;
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
@@ -102,16 +102,12 @@ impl<'tcx> LateLintPass<'tcx> for LegacyNumericConstants {
102102
}
103103

104104
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
105-
let ExprKind::Path(qpath) = &expr.kind else {
106-
return;
107-
};
108-
109105
// `std::<integer>::<CONST>` check
110-
let (span, sugg, msg) = if let QPath::Resolved(None, path) = qpath
106+
let (span, sugg, msg) = if let ExprKind::Path(qpath) = &expr.kind
107+
&& let QPath::Resolved(None, path) = qpath
111108
&& let Some(def_id) = path.res.opt_def_id()
112109
&& is_numeric_const(cx, def_id)
113-
&& let def_path = cx.get_def_path(def_id)
114-
&& let [.., mod_name, name] = &*def_path
110+
&& let [.., mod_name, name] = &*cx.get_def_path(def_id)
115111
// Skip linting if this usage looks identical to the associated constant,
116112
// since this would only require removing a `use` import (which is already linted).
117113
&& !is_numeric_const_path_canonical(path, [*mod_name, *name])
@@ -122,16 +118,16 @@ impl<'tcx> LateLintPass<'tcx> for LegacyNumericConstants {
122118
"usage of a legacy numeric constant",
123119
)
124120
// `<integer>::xxx_value` check
125-
} else if let QPath::TypeRelative(mod_path, last_segment) = qpath
126-
&& let Some(def_id) = cx.qpath_res(qpath, expr.hir_id).opt_def_id()
127-
&& let Some(par_expr) = get_parent_expr(cx, expr)
128-
&& let ExprKind::Call(_, []) = par_expr.kind
121+
} else if let ExprKind::Call(func, []) = &expr.kind
122+
&& let ExprKind::Path(qpath) = &func.kind
123+
&& let QPath::TypeRelative(ty, last_segment) = qpath
124+
&& let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id()
129125
&& is_integer_method(cx, def_id)
130126
{
131127
let name = last_segment.ident.name.as_str();
132-
let mod_name = clippy_utils::source::snippet(cx, mod_path.span, "_");
128+
let mod_name = clippy_utils::source::snippet(cx, ty.span, "_");
133129
(
134-
par_expr.span,
130+
expr.span,
135131
format!("{}::{}", mod_name, name[..=2].to_ascii_uppercase()),
136132
"usage of a legacy numeric method",
137133
)

0 commit comments

Comments
 (0)