Skip to content

Commit f1b9105

Browse files
committed
Auto merge of rust-lang#10454 - Alexendoo:transmutes-expressible-as-ptr-casts-parens, r=Jarcho
Wrap `transmutes_expressible_as_ptr_casts` suggestions in parentheses changelog: [`transmutes_expressible_as_ptr_casts`]: Fix suggestion missing wrapping parentheses Fixes rust-lang#10449 r? `@Jarcho` Is this the best way to go about this? `unused_parens` will catch the unnecessary ones but emitting them in the first place isn't ideal
2 parents ed6c15a + ecc2012 commit f1b9105

4 files changed

+30
-5
lines changed

clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use super::utils::check_cast;
22
use super::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS;
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::sugg::Sugg;
5+
use rustc_ast::ExprPrecedence;
56
use rustc_errors::Applicability;
6-
use rustc_hir::Expr;
7+
use rustc_hir::{Expr, Node};
78
use rustc_lint::LateContext;
89
use rustc_middle::ty::{cast::CastKind, Ty};
910

@@ -19,7 +20,7 @@ pub(super) fn check<'tcx>(
1920
) -> bool {
2021
use CastKind::{AddrPtrCast, ArrayPtrCast, FnPtrAddrCast, FnPtrPtrCast, PtrAddrCast, PtrPtrCast};
2122
let mut app = Applicability::MachineApplicable;
22-
let sugg = match check_cast(cx, e, from_ty, to_ty) {
23+
let mut sugg = match check_cast(cx, e, from_ty, to_ty) {
2324
Some(PtrPtrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast) => {
2425
Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app)
2526
.as_ty(to_ty.to_string())
@@ -39,6 +40,12 @@ pub(super) fn check<'tcx>(
3940
_ => return false,
4041
};
4142

43+
if let Node::Expr(parent) = cx.tcx.hir().get_parent(e.hir_id)
44+
&& parent.precedence().order() > ExprPrecedence::Cast.order()
45+
{
46+
sugg = format!("({sugg})");
47+
}
48+
4249
span_lint_and_sugg(
4350
cx,
4451
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,

tests/ui/transmutes_expressible_as_ptr_casts.fixed

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// would otherwise be responsible for
55
#![warn(clippy::useless_transmute)]
66
#![warn(clippy::transmute_ptr_to_ptr)]
7-
#![allow(dead_code, unused_unsafe, clippy::borrow_as_ptr)]
7+
#![allow(unused, clippy::borrow_as_ptr)]
88

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

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

7878
unsafe { transmute::<Single, Pair>(in_param) }
7979
}
80+
81+
fn issue_10449() {
82+
fn f() {}
83+
84+
let _x: u8 = unsafe { *(f as *const u8) };
85+
}

tests/ui/transmutes_expressible_as_ptr_casts.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// would otherwise be responsible for
55
#![warn(clippy::useless_transmute)]
66
#![warn(clippy::transmute_ptr_to_ptr)]
7-
#![allow(dead_code, unused_unsafe, clippy::borrow_as_ptr)]
7+
#![allow(unused, clippy::borrow_as_ptr)]
88

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

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

7878
unsafe { transmute::<Single, Pair>(in_param) }
7979
}
80+
81+
fn issue_10449() {
82+
fn f() {}
83+
84+
let _x: u8 = unsafe { *std::mem::transmute::<fn(), *const u8>(f) };
85+
}

tests/ui/transmutes_expressible_as_ptr_casts.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,11 @@ error: transmute from a reference to a pointer
5858
LL | unsafe { transmute::<&[i32; 1], *const u8>(in_param) }
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `in_param as *const [i32; 1] as *const u8`
6060

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

0 commit comments

Comments
 (0)