Skip to content

Commit

Permalink
Wrap transmutes_expressible_as_ptr_casts suggestions in parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexendoo committed Mar 5, 2023
1 parent 70e85d1 commit 1a90c7d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ pub(super) fn check<'tcx>(
Some(PtrPtrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast) => {
Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app)
.as_ty(to_ty.to_string())
.maybe_par()
.to_string()
},
Some(PtrAddrCast) if !from_ty_adjusted => Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app)
.as_ty(to_ty.to_string())
.maybe_par()
.to_string(),

// The only adjustments here would be ref-to-ptr and unsize coercions. The result of an unsize coercions can't
// be transmuted to a usize. For ref-to-ptr coercions, borrows need to be cast to a pointer before being cast to
// a usize.
Some(PtrAddrCast) => format!(
"{} as {to_ty}",
"({} as {to_ty})",
Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app).as_ty(from_ty)
),
_ => return false,
Expand Down
16 changes: 11 additions & 5 deletions tests/ui/transmutes_expressible_as_ptr_casts.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// would otherwise be responsible for
#![warn(clippy::useless_transmute)]
#![warn(clippy::transmute_ptr_to_ptr)]
#![allow(dead_code, unused_unsafe, clippy::borrow_as_ptr)]
#![allow(unused, clippy::borrow_as_ptr)]

use std::mem::{size_of, transmute};

Expand All @@ -31,7 +31,7 @@ fn main() {
// we should wait until std::raw::TraitObject is stabilized?

// e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast
let _usize_from_int_ptr_transmute = unsafe { ptr_i32 as usize };
let _usize_from_int_ptr_transmute = unsafe { (ptr_i32 as usize) };
let _usize_from_int_ptr = ptr_i32 as usize;

let array_ref: &[i32; 4] = &[1, 2, 3, 4];
Expand All @@ -45,14 +45,14 @@ fn main() {
}

// e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast
let _usize_ptr_transmute = unsafe { foo as *const usize };
let _usize_ptr_transmute = unsafe { (foo as *const usize) };
let _usize_ptr_transmute = foo as *const usize;

// e is a function pointer type and U is an integer; fptr-addr-cast
let _usize_from_fn_ptr_transmute = unsafe { foo as usize };
let _usize_from_fn_ptr_transmute = unsafe { (foo as usize) };
let _usize_from_fn_ptr = foo as *const usize;

let _usize_from_ref = unsafe { &1u32 as *const u32 as usize };
let _usize_from_ref = unsafe { (&1u32 as *const u32 as usize) };
}

// If a ref-to-ptr cast of this form where the pointer type points to a type other
Expand All @@ -77,3 +77,9 @@ fn cannot_be_expressed_as_pointer_cast(in_param: Single) -> Pair {

unsafe { transmute::<Single, Pair>(in_param) }
}

fn issue_10449() {
fn f() {}

let _x: u8 = unsafe { *(f as *const u8) };
}
8 changes: 7 additions & 1 deletion tests/ui/transmutes_expressible_as_ptr_casts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// would otherwise be responsible for
#![warn(clippy::useless_transmute)]
#![warn(clippy::transmute_ptr_to_ptr)]
#![allow(dead_code, unused_unsafe, clippy::borrow_as_ptr)]
#![allow(unused, clippy::borrow_as_ptr)]

use std::mem::{size_of, transmute};

Expand Down Expand Up @@ -77,3 +77,9 @@ fn cannot_be_expressed_as_pointer_cast(in_param: Single) -> Pair {

unsafe { transmute::<Single, Pair>(in_param) }
}

fn issue_10449() {
fn f() {}

let _x: u8 = unsafe { *std::mem::transmute::<fn(), *const u8>(f) };
}
16 changes: 11 additions & 5 deletions tests/ui/transmutes_expressible_as_ptr_casts.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ error: transmute from `*const i32` to `usize` which could be expressed as a poin
--> $DIR/transmutes_expressible_as_ptr_casts.rs:34:50
|
LL | let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(ptr_i32 as usize)`
|
= note: `-D clippy::transmutes-expressible-as-ptr-casts` implied by `-D warnings`

Expand All @@ -38,25 +38,31 @@ error: transmute from `fn(usize) -> u8` to `*const usize` which could be express
--> $DIR/transmutes_expressible_as_ptr_casts.rs:48:41
|
LL | let _usize_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, *const usize>(foo) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(foo as *const usize)`

error: transmute from `fn(usize) -> u8` to `usize` which could be expressed as a pointer cast instead
--> $DIR/transmutes_expressible_as_ptr_casts.rs:52:49
|
LL | let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(foo as usize)`

error: transmute from `*const u32` to `usize` which could be expressed as a pointer cast instead
--> $DIR/transmutes_expressible_as_ptr_casts.rs:55:36
|
LL | let _usize_from_ref = unsafe { transmute::<*const u32, usize>(&1u32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&1u32 as *const u32 as usize`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(&1u32 as *const u32 as usize)`

error: transmute from a reference to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:66:14
|
LL | unsafe { transmute::<&[i32; 1], *const u8>(in_param) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `in_param as *const [i32; 1] as *const u8`

error: aborting due to 9 previous errors
error: transmute from `fn()` to `*const u8` which could be expressed as a pointer cast instead
--> $DIR/transmutes_expressible_as_ptr_casts.rs:84:28
|
LL | let _x: u8 = unsafe { *std::mem::transmute::<fn(), *const u8>(f) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(f as *const u8)`

error: aborting due to 10 previous errors

0 comments on commit 1a90c7d

Please sign in to comment.