fix: invalid_upcast_comparisons wrongly unmangled macros

This commit is contained in:
yanglsh 2025-08-25 08:20:01 +08:00
parent 368b235579
commit 066a9794df
3 changed files with 30 additions and 3 deletions

View file

@ -1,3 +1,4 @@
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::layout::LayoutOf;
@ -9,7 +10,7 @@ use clippy_utils::comparisons;
use clippy_utils::comparisons::Rel;
use clippy_utils::consts::{ConstEvalCtxt, FullInt};
use clippy_utils::diagnostics::span_lint;
use clippy_utils::source::snippet;
use clippy_utils::source::snippet_with_context;
declare_clippy_lint! {
/// ### What it does
@ -69,13 +70,21 @@ fn numeric_cast_precast_bounds(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<
fn err_upcast_comparison(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, always: bool) {
if let ExprKind::Cast(cast_val, _) = expr.kind {
let mut applicability = Applicability::MachineApplicable;
let (cast_val_snip, _) = snippet_with_context(
cx,
cast_val.span,
expr.span.ctxt(),
"the expression",
&mut applicability,
);
span_lint(
cx,
INVALID_UPCAST_COMPARISONS,
span,
format!(
"because of the numeric bounds on `{}` prior to casting, this expression is always {}",
snippet(cx, cast_val.span, "the expression"),
cast_val_snip,
if always { "true" } else { "false" },
),
);

View file

@ -133,3 +133,15 @@ fn main() {
-5 == (u32 as i32);
}
fn issue15662() {
macro_rules! add_one {
($x:expr) => {
$x + 1
};
}
let x: u8 = 1;
(add_one!(x) as u32) > 300;
//~^ invalid_upcast_comparisons
}

View file

@ -163,5 +163,11 @@ error: because of the numeric bounds on `u8` prior to casting, this expression i
LL | -5 >= (u8 as i32);
| ^^^^^^^^^^^^^^^^^
error: aborting due to 27 previous errors
error: because of the numeric bounds on `add_one!(x)` prior to casting, this expression is always false
--> tests/ui/invalid_upcast_comparisons.rs:145:5
|
LL | (add_one!(x) as u32) > 300;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 28 previous errors