Skip to content

Commit 0ba022a

Browse files
committed
legacy_numeric_constants: add ctxt check for internal macro
changelog: none Signed-off-by: Zihan <[email protected]>
1 parent 8229701 commit 0ba022a

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

clippy_lints/src/legacy_numeric_constants.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -113,35 +113,18 @@ impl<'tcx> LateLintPass<'tcx> for LegacyNumericConstants {
113113
// since this would only require removing a `use` import (which is already linted).
114114
&& !is_numeric_const_path_canonical(path, [*mod_name, *name])
115115
{
116-
(
117-
vec![(expr.span, format!("{mod_name}::{name}"))],
118-
"usage of a legacy numeric constant",
119-
)
116+
(format!("{mod_name}::{name}"), "usage of a legacy numeric constant")
120117
// `<integer>::xxx_value` check
121118
} else if let ExprKind::Call(func, []) = &expr.kind
122119
&& let ExprKind::Path(qpath) = &func.kind
123120
&& let QPath::TypeRelative(ty, last_segment) = qpath
124121
&& let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id()
125122
&& is_integer_method(cx, def_id)
123+
&& let Some(mod_name) = ty.span.get_source_text(cx)
124+
&& ty.span.eq_ctxt(last_segment.ident.span)
126125
{
127-
let mut sugg = vec![
128-
// Replace the function name up to the end by the constant name
129-
(
130-
last_segment.ident.span.to(expr.span.shrink_to_hi()),
131-
last_segment.ident.name.as_str()[..=2].to_ascii_uppercase(),
132-
),
133-
];
134-
let before_span = expr.span.shrink_to_lo().until(ty.span);
135-
if !before_span.is_empty() {
136-
// Remove everything before the type name
137-
sugg.push((before_span, String::new()));
138-
}
139-
// Use `::` between the type name and the constant
140-
let between_span = ty.span.shrink_to_hi().until(last_segment.ident.span);
141-
if !between_span.check_source_text(cx, |s| s == "::") {
142-
sugg.push((between_span, String::from("::")));
143-
}
144-
(sugg, "usage of a legacy numeric method")
126+
let name = last_segment.ident.name.as_str()[..=2].to_ascii_uppercase();
127+
(format!("{mod_name}::{name}"), "usage of a legacy numeric method")
145128
} else {
146129
return;
147130
};
@@ -151,7 +134,8 @@ impl<'tcx> LateLintPass<'tcx> for LegacyNumericConstants {
151134
&& !is_from_proc_macro(cx, expr)
152135
{
153136
span_lint_and_then(cx, LEGACY_NUMERIC_CONSTANTS, expr.span, msg, |diag| {
154-
diag.multipart_suggestion_verbose(
137+
diag.span_suggestion_verbose(
138+
expr.span,
155139
"use the associated constant instead",
156140
sugg,
157141
Applicability::MaybeIncorrect,

tests/ui/legacy_numeric_constants_unfixable.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,14 @@ fn msrv_juust_right() {
7777
use std::u32::MAX;
7878
//~^ ERROR: importing a legacy numeric constant
7979
}
80+
81+
macro_rules! foo {
82+
($a: ty) => {
83+
let _ = <$a>::max_value();
84+
let _ = (<$a>::max_value)();
85+
};
86+
}
87+
88+
fn issue15805() {
89+
foo!(u8);
90+
}

0 commit comments

Comments
 (0)