Skip to content

Commit 066a979

Browse files
committed
fix: invalid_upcast_comparisons wrongly unmangled macros
1 parent 368b235 commit 066a979

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

clippy_lints/src/invalid_upcast_comparisons.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_errors::Applicability;
12
use rustc_hir::{Expr, ExprKind};
23
use rustc_lint::{LateContext, LateLintPass};
34
use rustc_middle::ty::layout::LayoutOf;
@@ -9,7 +10,7 @@ use clippy_utils::comparisons;
910
use clippy_utils::comparisons::Rel;
1011
use clippy_utils::consts::{ConstEvalCtxt, FullInt};
1112
use clippy_utils::diagnostics::span_lint;
12-
use clippy_utils::source::snippet;
13+
use clippy_utils::source::snippet_with_context;
1314

1415
declare_clippy_lint! {
1516
/// ### What it does
@@ -69,13 +70,21 @@ fn numeric_cast_precast_bounds(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<
6970

7071
fn err_upcast_comparison(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, always: bool) {
7172
if let ExprKind::Cast(cast_val, _) = expr.kind {
73+
let mut applicability = Applicability::MachineApplicable;
74+
let (cast_val_snip, _) = snippet_with_context(
75+
cx,
76+
cast_val.span,
77+
expr.span.ctxt(),
78+
"the expression",
79+
&mut applicability,
80+
);
7281
span_lint(
7382
cx,
7483
INVALID_UPCAST_COMPARISONS,
7584
span,
7685
format!(
7786
"because of the numeric bounds on `{}` prior to casting, this expression is always {}",
78-
snippet(cx, cast_val.span, "the expression"),
87+
cast_val_snip,
7988
if always { "true" } else { "false" },
8089
),
8190
);

tests/ui/invalid_upcast_comparisons.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,15 @@ fn main() {
133133

134134
-5 == (u32 as i32);
135135
}
136+
137+
fn issue15662() {
138+
macro_rules! add_one {
139+
($x:expr) => {
140+
$x + 1
141+
};
142+
}
143+
144+
let x: u8 = 1;
145+
(add_one!(x) as u32) > 300;
146+
//~^ invalid_upcast_comparisons
147+
}

tests/ui/invalid_upcast_comparisons.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,11 @@ error: because of the numeric bounds on `u8` prior to casting, this expression i
163163
LL | -5 >= (u8 as i32);
164164
| ^^^^^^^^^^^^^^^^^
165165

166-
error: aborting due to 27 previous errors
166+
error: because of the numeric bounds on `add_one!(x)` prior to casting, this expression is always false
167+
--> tests/ui/invalid_upcast_comparisons.rs:145:5
168+
|
169+
LL | (add_one!(x) as u32) > 300;
170+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
171+
172+
error: aborting due to 28 previous errors
167173

0 commit comments

Comments
 (0)