Skip to content

Commit f464149

Browse files
committed
Auto merge of rust-lang#11516 - mojave2:issue-11458, r=giraffate
fix cast_lossless with macro call changelog: fix [`cast_lossless`] in the case when the cast operand is a macro call fix rust-lang#11458
2 parents 889e1b9 + 3cad623 commit f464149

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

Diff for: clippy_lints/src/casts/cast_lossless.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(super) fn check(
2525
// The suggestion is to use a function call, so if the original expression
2626
// has parens on the outside, they are no longer needed.
2727
let mut applicability = Applicability::MachineApplicable;
28-
let opt = snippet_opt(cx, cast_op.span);
28+
let opt = snippet_opt(cx, cast_op.span.source_callsite());
2929
let sugg = opt.as_ref().map_or_else(
3030
|| {
3131
applicability = Applicability::HasPlaceholders;

Diff for: tests/ui/cast_lossless_integer.fixed

+11
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,14 @@ mod cast_lossless_in_impl {
4949
enum Test {
5050
A = u32::MAX as i64 + 1,
5151
}
52+
53+
fn issue11458() {
54+
macro_rules! sign_cast {
55+
($var: ident, $src: ty, $dest: ty) => {
56+
<$dest>::from_ne_bytes(($var as $src).to_ne_bytes())
57+
};
58+
}
59+
let x = 10_u128;
60+
let _ = i32::from(sign_cast!(x, u8, i8));
61+
let _ = i32::from(sign_cast!(x, u8, i8) + 1);
62+
}

Diff for: tests/ui/cast_lossless_integer.rs

+11
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,14 @@ mod cast_lossless_in_impl {
4949
enum Test {
5050
A = u32::MAX as i64 + 1,
5151
}
52+
53+
fn issue11458() {
54+
macro_rules! sign_cast {
55+
($var: ident, $src: ty, $dest: ty) => {
56+
<$dest>::from_ne_bytes(($var as $src).to_ne_bytes())
57+
};
58+
}
59+
let x = 10_u128;
60+
let _ = sign_cast!(x, u8, i8) as i32;
61+
let _ = (sign_cast!(x, u8, i8) + 1) as i32;
62+
}

Diff for: tests/ui/cast_lossless_integer.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,17 @@ error: casting `u8` to `u16` may become silently lossy if you later change the t
115115
LL | let _ = (1u8 + 1u8) as u16;
116116
| ^^^^^^^^^^^^^^^^^^ help: try: `u16::from(1u8 + 1u8)`
117117

118-
error: aborting due to 19 previous errors
118+
error: casting `i8` to `i32` may become silently lossy if you later change the type
119+
--> $DIR/cast_lossless_integer.rs:60:13
120+
|
121+
LL | let _ = sign_cast!(x, u8, i8) as i32;
122+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::from(sign_cast!(x, u8, i8))`
123+
124+
error: casting `i8` to `i32` may become silently lossy if you later change the type
125+
--> $DIR/cast_lossless_integer.rs:61:13
126+
|
127+
LL | let _ = (sign_cast!(x, u8, i8) + 1) as i32;
128+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::from(sign_cast!(x, u8, i8) + 1)`
129+
130+
error: aborting due to 21 previous errors
119131

0 commit comments

Comments
 (0)