Skip to content

Commit 398c33c

Browse files
committed
fix: cast-lossless should not suggest when casting type is from macro input
1 parent 0397819 commit 398c33c

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

clippy_lints/src/casts/cast_lossless.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ pub(super) fn check(
4646
applicability,
4747
);
4848
},
49+
_ if expr.span.from_expansion() && !cast_to_hir.span.eq_ctxt(expr.span) => {
50+
diag.span_help(
51+
expr.span,
52+
format!("use `<{ty}>::from` instead")
53+
);
54+
},
4955
// Don't suggest `A<_>::B::From(x)` or `macro!()::from(x)`
5056
kind if matches!(kind, TyKind::Path(QPath::Resolved(_, path)) if path.segments.iter().any(|s| s.args.is_some()))
5157
|| !cast_to_hir.span.eq_ctxt(expr.span) =>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![warn(clippy::cast_lossless)]
2+
3+
fn issue15348() {
4+
macro_rules! zero {
5+
($int:ty) => {{
6+
let data: [u8; 3] = [0, 0, 0];
7+
data[0] as $int
8+
//~^ cast_lossless
9+
//~| cast_lossless
10+
//~| cast_lossless
11+
//~| cast_lossless
12+
}};
13+
}
14+
15+
let _ = zero!(u16);
16+
let _ = zero!(u32);
17+
let _ = zero!(u64);
18+
let _ = zero!(u128);
19+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
error: casts from `u8` to `u16` can be expressed infallibly using `From`
2+
--> tests/ui/cast_lossless_integer_unfixable.rs:7:13
3+
|
4+
LL | data[0] as $int
5+
| ^^^^^^^^^^^^^^^
6+
...
7+
LL | let _ = zero!(u16);
8+
| ---------- in this macro invocation
9+
|
10+
= help: an `as` cast can become silently lossy if the types change in the future
11+
help: use `<u16>::from` instead
12+
--> tests/ui/cast_lossless_integer_unfixable.rs:7:13
13+
|
14+
LL | data[0] as $int
15+
| ^^^^^^^^^^^^^^^
16+
...
17+
LL | let _ = zero!(u16);
18+
| ---------- in this macro invocation
19+
= note: `-D clippy::cast-lossless` implied by `-D warnings`
20+
= help: to override `-D warnings` add `#[allow(clippy::cast_lossless)]`
21+
= note: this error originates in the macro `zero` (in Nightly builds, run with -Z macro-backtrace for more info)
22+
23+
error: casts from `u8` to `u32` can be expressed infallibly using `From`
24+
--> tests/ui/cast_lossless_integer_unfixable.rs:7:13
25+
|
26+
LL | data[0] as $int
27+
| ^^^^^^^^^^^^^^^
28+
...
29+
LL | let _ = zero!(u32);
30+
| ---------- in this macro invocation
31+
|
32+
= help: an `as` cast can become silently lossy if the types change in the future
33+
help: use `<u32>::from` instead
34+
--> tests/ui/cast_lossless_integer_unfixable.rs:7:13
35+
|
36+
LL | data[0] as $int
37+
| ^^^^^^^^^^^^^^^
38+
...
39+
LL | let _ = zero!(u32);
40+
| ---------- in this macro invocation
41+
= note: this error originates in the macro `zero` (in Nightly builds, run with -Z macro-backtrace for more info)
42+
43+
error: casts from `u8` to `u64` can be expressed infallibly using `From`
44+
--> tests/ui/cast_lossless_integer_unfixable.rs:7:13
45+
|
46+
LL | data[0] as $int
47+
| ^^^^^^^^^^^^^^^
48+
...
49+
LL | let _ = zero!(u64);
50+
| ---------- in this macro invocation
51+
|
52+
= help: an `as` cast can become silently lossy if the types change in the future
53+
help: use `<u64>::from` instead
54+
--> tests/ui/cast_lossless_integer_unfixable.rs:7:13
55+
|
56+
LL | data[0] as $int
57+
| ^^^^^^^^^^^^^^^
58+
...
59+
LL | let _ = zero!(u64);
60+
| ---------- in this macro invocation
61+
= note: this error originates in the macro `zero` (in Nightly builds, run with -Z macro-backtrace for more info)
62+
63+
error: casts from `u8` to `u128` can be expressed infallibly using `From`
64+
--> tests/ui/cast_lossless_integer_unfixable.rs:7:13
65+
|
66+
LL | data[0] as $int
67+
| ^^^^^^^^^^^^^^^
68+
...
69+
LL | let _ = zero!(u128);
70+
| ----------- in this macro invocation
71+
|
72+
= help: an `as` cast can become silently lossy if the types change in the future
73+
help: use `<u128>::from` instead
74+
--> tests/ui/cast_lossless_integer_unfixable.rs:7:13
75+
|
76+
LL | data[0] as $int
77+
| ^^^^^^^^^^^^^^^
78+
...
79+
LL | let _ = zero!(u128);
80+
| ----------- in this macro invocation
81+
= note: this error originates in the macro `zero` (in Nightly builds, run with -Z macro-backtrace for more info)
82+
83+
error: aborting due to 4 previous errors
84+

0 commit comments

Comments
 (0)