Skip to content

Commit a55517f

Browse files
Fix cast-lossless should not suggest when casting type is from macro input (#15358)
Closes #15348 The span of the `as` expr is also incorrect, but I believe it is not a bug from Clippy. changelog: [`cast-lossless`] fix wrong suggestions when casting type is from macro input
2 parents f4f579f + 938e79f commit a55517f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

clippy_lints/src/casts/cast_lossless.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ pub(super) fn check(
2525
return;
2626
}
2727

28+
// If the `as` is from a macro and the casting type is from macro input, whether it is lossless is
29+
// dependent on the input
30+
if expr.span.from_expansion() && !cast_to_hir.span.eq_ctxt(expr.span) {
31+
return;
32+
}
33+
2834
span_lint_and_then(
2935
cx,
3036
CAST_LOSSLESS,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@check-pass
2+
#![warn(clippy::cast_lossless)]
3+
4+
fn issue15348() {
5+
macro_rules! zero {
6+
($int:ty) => {{
7+
let data: [u8; 3] = [0, 0, 0];
8+
data[0] as $int
9+
}};
10+
}
11+
12+
let _ = zero!(u8);
13+
let _ = zero!(u16);
14+
let _ = zero!(u32);
15+
let _ = zero!(u64);
16+
let _ = zero!(u128);
17+
}

0 commit comments

Comments
 (0)