Skip to content

fix cast_lossless with macro call #11516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/src/casts/cast_lossless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(super) fn check(
// The suggestion is to use a function call, so if the original expression
// has parens on the outside, they are no longer needed.
let mut applicability = Applicability::MachineApplicable;
let opt = snippet_opt(cx, cast_op.span);
let opt = snippet_opt(cx, cast_op.span.source_callsite());
let sugg = opt.as_ref().map_or_else(
|| {
applicability = Applicability::HasPlaceholders;
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/cast_lossless_integer.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,14 @@ mod cast_lossless_in_impl {
enum Test {
A = u32::MAX as i64 + 1,
}

fn issue11458() {
macro_rules! sign_cast {
($var: ident, $src: ty, $dest: ty) => {
<$dest>::from_ne_bytes(($var as $src).to_ne_bytes())
};
}
let x = 10_u128;
let _ = i32::from(sign_cast!(x, u8, i8));
let _ = i32::from(sign_cast!(x, u8, i8) + 1);
}
11 changes: 11 additions & 0 deletions tests/ui/cast_lossless_integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,14 @@ mod cast_lossless_in_impl {
enum Test {
A = u32::MAX as i64 + 1,
}

fn issue11458() {
macro_rules! sign_cast {
($var: ident, $src: ty, $dest: ty) => {
<$dest>::from_ne_bytes(($var as $src).to_ne_bytes())
};
}
let x = 10_u128;
let _ = sign_cast!(x, u8, i8) as i32;
let _ = (sign_cast!(x, u8, i8) + 1) as i32;
}
14 changes: 13 additions & 1 deletion tests/ui/cast_lossless_integer.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,17 @@ error: casting `u8` to `u16` may become silently lossy if you later change the t
LL | let _ = (1u8 + 1u8) as u16;
| ^^^^^^^^^^^^^^^^^^ help: try: `u16::from(1u8 + 1u8)`

error: aborting due to 19 previous errors
error: casting `i8` to `i32` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:60:13
|
LL | let _ = sign_cast!(x, u8, i8) as i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::from(sign_cast!(x, u8, i8))`

error: casting `i8` to `i32` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:61:13
|
LL | let _ = (sign_cast!(x, u8, i8) + 1) as i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::from(sign_cast!(x, u8, i8) + 1)`

error: aborting due to 21 previous errors