Skip to content

Commit d4cb0b4

Browse files
committed
clean-up a bit
- if-else reordering: having one fewer nesting levels makes it easier to understand imo
1 parent 828ee51 commit d4cb0b4

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

clippy_lints/src/transmute/transmute_ptr_to_ref.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ pub(super) fn check<'tcx>(
2828
format!("transmute from a pointer type (`{from_ty}`) to a reference type (`{to_ty}`)"),
2929
|diag| {
3030
let arg = sugg::Sugg::hir(cx, arg, "..");
31-
let (deref, cast) = if *mutbl == Mutability::Mut {
32-
("&mut *", "*mut")
33-
} else {
34-
("&*", "*const")
31+
let (deref, cast) = match mutbl {
32+
Mutability::Mut => ("&mut *", "*mut"),
33+
Mutability::Not => ("&*", "*const"),
3534
};
3635
let mut app = Applicability::MachineApplicable;
3736

@@ -45,15 +44,12 @@ pub(super) fn check<'tcx>(
4544
sugg::make_unop(deref, arg.as_ty(format!("{cast} {ty_snip}"))).to_string()
4645
}
4746
} else if *from_ptr_ty == *to_ref_ty {
48-
if from_ptr_ty.has_erased_regions() {
49-
if msrv.meets(cx, msrvs::POINTER_CAST) {
50-
format!("{deref}{}.cast::<{to_ref_ty}>()", arg.maybe_paren())
51-
} else {
52-
sugg::make_unop(deref, arg.as_ty(format!("{cast} () as {cast} {to_ref_ty}")))
53-
.to_string()
54-
}
55-
} else {
47+
if !from_ptr_ty.has_erased_regions() {
5648
sugg::make_unop(deref, arg).to_string()
49+
} else if msrv.meets(cx, msrvs::POINTER_CAST) {
50+
format!("{deref}{}.cast::<{to_ref_ty}>()", arg.maybe_paren())
51+
} else {
52+
sugg::make_unop(deref, arg.as_ty(format!("{cast} () as {cast} {to_ref_ty}"))).to_string()
5753
}
5854
} else {
5955
sugg::make_unop(deref, arg.as_ty(format!("{cast} {to_ref_ty}"))).to_string()

tests/ui/transmute_ptr_to_ref.fixed

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
clippy::missing_transmute_annotations
66
)]
77

8-
unsafe fn _ptr_to_ref<T, U>(p: *const T, m: *mut T, o: *const U, om: *mut U) {
8+
fn ptr_to_ref<T, U>(p: *const T, m: *mut T, o: *const U, om: *mut U) {
99
unsafe {
1010
let _: &T = &*p;
1111
//~^ transmute_ptr_to_ref
@@ -37,7 +37,7 @@ unsafe fn _ptr_to_ref<T, U>(p: *const T, m: *mut T, o: *const U, om: *mut U) {
3737
}
3838
}
3939

40-
fn _issue1231() {
40+
fn issue1231() {
4141
struct Foo<'a, T> {
4242
bar: &'a T,
4343
}
@@ -55,7 +55,7 @@ fn _issue1231() {
5555
//~^ transmute_ptr_to_ref
5656
}
5757

58-
unsafe fn _issue8924<'a, 'b, 'c>(x: *const &'a u32, y: *const &'b u32) -> &'c &'b u32 {
58+
fn issue8924<'a, 'b, 'c>(x: *const &'a u32, y: *const &'b u32) -> &'c &'b u32 {
5959
unsafe {
6060
match 0 {
6161
0 => &*x.cast::<&u32>(),
@@ -71,7 +71,7 @@ unsafe fn _issue8924<'a, 'b, 'c>(x: *const &'a u32, y: *const &'b u32) -> &'c &'
7171
}
7272

7373
#[clippy::msrv = "1.38"]
74-
unsafe fn _meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 {
74+
fn meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 {
7575
unsafe {
7676
let a = 0u32;
7777
let a = &a as *const u32;
@@ -89,7 +89,7 @@ unsafe fn _meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 {
8989
}
9090

9191
#[clippy::msrv = "1.37"]
92-
unsafe fn _under_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 {
92+
fn under_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 {
9393
unsafe {
9494
let a = 0u32;
9595
let a = &a as *const u32;

tests/ui/transmute_ptr_to_ref.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
clippy::missing_transmute_annotations
66
)]
77

8-
unsafe fn _ptr_to_ref<T, U>(p: *const T, m: *mut T, o: *const U, om: *mut U) {
8+
fn ptr_to_ref<T, U>(p: *const T, m: *mut T, o: *const U, om: *mut U) {
99
unsafe {
1010
let _: &T = std::mem::transmute(p);
1111
//~^ transmute_ptr_to_ref
@@ -37,7 +37,7 @@ unsafe fn _ptr_to_ref<T, U>(p: *const T, m: *mut T, o: *const U, om: *mut U) {
3737
}
3838
}
3939

40-
fn _issue1231() {
40+
fn issue1231() {
4141
struct Foo<'a, T> {
4242
bar: &'a T,
4343
}
@@ -55,7 +55,7 @@ fn _issue1231() {
5555
//~^ transmute_ptr_to_ref
5656
}
5757

58-
unsafe fn _issue8924<'a, 'b, 'c>(x: *const &'a u32, y: *const &'b u32) -> &'c &'b u32 {
58+
fn issue8924<'a, 'b, 'c>(x: *const &'a u32, y: *const &'b u32) -> &'c &'b u32 {
5959
unsafe {
6060
match 0 {
6161
0 => std::mem::transmute(x),
@@ -71,7 +71,7 @@ unsafe fn _issue8924<'a, 'b, 'c>(x: *const &'a u32, y: *const &'b u32) -> &'c &'
7171
}
7272

7373
#[clippy::msrv = "1.38"]
74-
unsafe fn _meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 {
74+
fn meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 {
7575
unsafe {
7676
let a = 0u32;
7777
let a = &a as *const u32;
@@ -89,7 +89,7 @@ unsafe fn _meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 {
8989
}
9090

9191
#[clippy::msrv = "1.37"]
92-
unsafe fn _under_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 {
92+
fn under_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 {
9393
unsafe {
9494
let a = 0u32;
9595
let a = &a as *const u32;

tests/ui/transmute_ptr_to_ref.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ error: transmute from a pointer type (`*mut U`) to a reference type (`&T`)
4343
LL | let _: &T = std::mem::transmute(om);
4444
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(om as *const T)`
4545

46-
error: transmute from a pointer type (`*const i32`) to a reference type (`&_issue1231::Foo<'_, u8>`)
46+
error: transmute from a pointer type (`*const i32`) to a reference type (`&issue1231::Foo<'_, u8>`)
4747
--> tests/ui/transmute_ptr_to_ref.rs:46:32
4848
|
4949
LL | let _: &Foo<u8> = unsafe { std::mem::transmute::<_, &Foo<_>>(raw) };
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*raw.cast::<Foo<_>>()`
5151

52-
error: transmute from a pointer type (`*const i32`) to a reference type (`&_issue1231::Foo<'_, &u8>`)
52+
error: transmute from a pointer type (`*const i32`) to a reference type (`&issue1231::Foo<'_, &u8>`)
5353
--> tests/ui/transmute_ptr_to_ref.rs:49:33
5454
|
5555
LL | let _: &Foo<&u8> = unsafe { std::mem::transmute::<_, &Foo<&_>>(raw) };

0 commit comments

Comments
 (0)