Skip to content
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

Warn on references casting to bigger memory layout #118983

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Avoid UB in clippy transmute_ptr_to_ptr UI test
  • Loading branch information
Urgau committed Feb 12, 2024
commit 746bb7e13610df9a9f88222a74302b93456c1675
6 changes: 3 additions & 3 deletions src/tools/clippy/tests/ui/transmute_ptr_to_ptr.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 35,16 @@ fn transmute_ptr_to_ptr() {
// ref-ref transmutes; bad
let _: &f32 = &*(&1u32 as *const u32 as *const f32);
//~^ ERROR: transmute from a reference to a reference
let _: &f64 = &*(&1f32 as *const f32 as *const f64);
let _: &f32 = &*(&1f64 as *const f64 as *const f32);
//~^ ERROR: transmute from a reference to a reference
//:^ this test is here because both f32 and f64 are the same TypeVariant, but they are not
// the same type
let _: &mut f32 = &mut *(&mut 1u32 as *mut u32 as *mut f32);
//~^ ERROR: transmute from a reference to a reference
let _: &GenericParam<f32> = &*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>);
//~^ ERROR: transmute from a reference to a reference
let u8_ref: &u8 = &0u8;
let u64_ref: &u64 = unsafe { &*(u8_ref as *const u8 as *const u64) };
let u64_ref: &u64 = &0u64;
let u8_ref: &u8 = unsafe { &*(u64_ref as *const u64 as *const u8) };
//~^ ERROR: transmute from a reference to a reference
}

Expand Down
6 changes: 3 additions & 3 deletions src/tools/clippy/tests/ui/transmute_ptr_to_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 35,16 @@ fn transmute_ptr_to_ptr() {
// ref-ref transmutes; bad
let _: &f32 = std::mem::transmute(&1u32);
//~^ ERROR: transmute from a reference to a reference
let _: &f64 = std::mem::transmute(&1f32);
let _: &f32 = std::mem::transmute(&1f64);
//~^ ERROR: transmute from a reference to a reference
//:^ this test is here because both f32 and f64 are the same TypeVariant, but they are not
// the same type
let _: &mut f32 = std::mem::transmute(&mut 1u32);
//~^ ERROR: transmute from a reference to a reference
let _: &GenericParam<f32> = std::mem::transmute(&GenericParam { t: 1u32 });
//~^ ERROR: transmute from a reference to a reference
let u8_ref: &u8 = &0u8;
let u64_ref: &u64 = unsafe { std::mem::transmute(u8_ref) };
let u64_ref: &u64 = &0u64;
let u8_ref: &u8 = unsafe { std::mem::transmute(u64_ref) };
flip1995 marked this conversation as resolved.
Show resolved Hide resolved
//~^ ERROR: transmute from a reference to a reference
}

Expand Down
10 changes: 5 additions & 5 deletions src/tools/clippy/tests/ui/transmute_ptr_to_ptr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 22,8 @@ LL | let _: &f32 = std::mem::transmute(&1u32);
error: transmute from a reference to a reference
--> $DIR/transmute_ptr_to_ptr.rs:38:23
|
LL | let _: &f64 = std::mem::transmute(&1f32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1f32 as *const f32 as *const f64)`
LL | let _: &f32 = std::mem::transmute(&1f64);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1f64 as *const f64 as *const f32)`

error: transmute from a reference to a reference
--> $DIR/transmute_ptr_to_ptr.rs:42:27
Expand All @@ -38,10 38,10 @@ LL | let _: &GenericParam<f32> = std::mem::transmute(&GenericParam { t:
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>)`

error: transmute from a reference to a reference
--> $DIR/transmute_ptr_to_ptr.rs:47:38
--> $DIR/transmute_ptr_to_ptr.rs:47:36
|
LL | let u64_ref: &u64 = unsafe { std::mem::transmute(u8_ref) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(u8_ref as *const u8 as *const u64)`
LL | let u8_ref: &u8 = unsafe { std::mem::transmute(u64_ref) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(u64_ref as *const u64 as *const u8)`

error: aborting due to 7 previous errors