Skip to content

Commit 18980df

Browse files
committed
Adjust clippy lints for rustc integer_to_ptr_transmutes lint
1 parent f1eb2c4 commit 18980df

File tree

6 files changed

+32
-67
lines changed

6 files changed

+32
-67
lines changed

src/tools/clippy/clippy_lints/src/transmute/useless_transmute.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,7 @@ pub(super) fn check<'tcx>(
4949
true
5050
},
5151
(ty::Int(_) | ty::Uint(_), ty::RawPtr(_, _)) => {
52-
span_lint_and_then(
53-
cx,
54-
USELESS_TRANSMUTE,
55-
e.span,
56-
"transmute from an integer to a pointer",
57-
|diag| {
58-
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
59-
diag.span_suggestion(e.span, "try", arg.as_ty(to_ty.to_string()), Applicability::Unspecified);
60-
}
61-
},
62-
);
52+
// Handled by the upstream rustc `integer_to_ptr_transmutes` lint
6353
true
6454
},
6555
_ => false,

src/tools/clippy/tests/ui/transmute.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
dead_code,
55
clippy::borrow_as_ptr,
66
unnecessary_transmutes,
7+
integer_to_ptr_transmutes,
78
clippy::needless_lifetimes,
89
clippy::missing_transmute_annotations
910
)]
@@ -60,12 +61,10 @@ fn useless() {
6061
//~^ useless_transmute
6162

6263
let _: *const usize = std::mem::transmute(5_isize);
63-
//~^ useless_transmute
6464

6565
let _ = std::ptr::dangling::<usize>();
6666

6767
let _: *const usize = std::mem::transmute(1 + 1usize);
68-
//~^ useless_transmute
6968

7069
let _ = (1 + 1_usize) as *const usize;
7170
}
Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: transmute from a reference to a pointer
2-
--> tests/ui/transmute.rs:33:27
2+
--> tests/ui/transmute.rs:34:27
33
|
44
LL | let _: *const T = core::mem::transmute(t);
55
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T`
@@ -8,61 +8,49 @@ LL | let _: *const T = core::mem::transmute(t);
88
= help: to override `-D warnings` add `#[allow(clippy::useless_transmute)]`
99

1010
error: transmute from a reference to a pointer
11-
--> tests/ui/transmute.rs:36:25
11+
--> tests/ui/transmute.rs:37:25
1212
|
1313
LL | let _: *mut T = core::mem::transmute(t);
1414
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T as *mut T`
1515

1616
error: transmute from a reference to a pointer
17-
--> tests/ui/transmute.rs:39:27
17+
--> tests/ui/transmute.rs:40:27
1818
|
1919
LL | let _: *const U = core::mem::transmute(t);
2020
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T as *const U`
2121

2222
error: transmute from a type (`std::vec::Vec<i32>`) to itself
23-
--> tests/ui/transmute.rs:47:27
23+
--> tests/ui/transmute.rs:48:27
2424
|
2525
LL | let _: Vec<i32> = core::mem::transmute(my_vec());
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727

2828
error: transmute from a type (`std::vec::Vec<i32>`) to itself
29-
--> tests/ui/transmute.rs:50:27
29+
--> tests/ui/transmute.rs:51:27
3030
|
3131
LL | let _: Vec<i32> = core::mem::transmute(my_vec());
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3333

3434
error: transmute from a type (`std::vec::Vec<i32>`) to itself
35-
--> tests/ui/transmute.rs:53:27
35+
--> tests/ui/transmute.rs:54:27
3636
|
3737
LL | let _: Vec<i32> = std::mem::transmute(my_vec());
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3939

4040
error: transmute from a type (`std::vec::Vec<i32>`) to itself
41-
--> tests/ui/transmute.rs:56:27
41+
--> tests/ui/transmute.rs:57:27
4242
|
4343
LL | let _: Vec<i32> = std::mem::transmute(my_vec());
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4545

4646
error: transmute from a type (`std::vec::Vec<i32>`) to itself
47-
--> tests/ui/transmute.rs:59:27
47+
--> tests/ui/transmute.rs:60:27
4848
|
4949
LL | let _: Vec<i32> = my_transmute(my_vec());
5050
| ^^^^^^^^^^^^^^^^^^^^^^
5151

52-
error: transmute from an integer to a pointer
53-
--> tests/ui/transmute.rs:62:31
54-
|
55-
LL | let _: *const usize = std::mem::transmute(5_isize);
56-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `5_isize as *const usize`
57-
58-
error: transmute from an integer to a pointer
59-
--> tests/ui/transmute.rs:67:31
60-
|
61-
LL | let _: *const usize = std::mem::transmute(1 + 1usize);
62-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(1 + 1usize) as *const usize`
63-
6452
error: transmute from a type (`*const Usize`) to the type that it points to (`Usize`)
65-
--> tests/ui/transmute.rs:99:24
53+
--> tests/ui/transmute.rs:98:24
6654
|
6755
LL | let _: Usize = core::mem::transmute(int_const_ptr);
6856
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -71,25 +59,25 @@ LL | let _: Usize = core::mem::transmute(int_const_ptr);
7159
= help: to override `-D warnings` add `#[allow(clippy::crosspointer_transmute)]`
7260

7361
error: transmute from a type (`*mut Usize`) to the type that it points to (`Usize`)
74-
--> tests/ui/transmute.rs:102:24
62+
--> tests/ui/transmute.rs:101:24
7563
|
7664
LL | let _: Usize = core::mem::transmute(int_mut_ptr);
7765
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7866

7967
error: transmute from a type (`Usize`) to a pointer to that type (`*const Usize`)
80-
--> tests/ui/transmute.rs:105:31
68+
--> tests/ui/transmute.rs:104:31
8169
|
8270
LL | let _: *const Usize = core::mem::transmute(my_int());
8371
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8472

8573
error: transmute from a type (`Usize`) to a pointer to that type (`*mut Usize`)
86-
--> tests/ui/transmute.rs:108:29
74+
--> tests/ui/transmute.rs:107:29
8775
|
8876
LL | let _: *mut Usize = core::mem::transmute(my_int());
8977
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9078

9179
error: transmute from a `u8` to a `bool`
92-
--> tests/ui/transmute.rs:115:28
80+
--> tests/ui/transmute.rs:114:28
9381
|
9482
LL | let _: bool = unsafe { std::mem::transmute(0_u8) };
9583
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `0_u8 != 0`
@@ -98,7 +86,7 @@ LL | let _: bool = unsafe { std::mem::transmute(0_u8) };
9886
= help: to override `-D warnings` add `#[allow(clippy::transmute_int_to_bool)]`
9987

10088
error: transmute from a `&[u8]` to a `&str`
101-
--> tests/ui/transmute.rs:122:28
89+
--> tests/ui/transmute.rs:121:28
10290
|
10391
LL | let _: &str = unsafe { std::mem::transmute(B) };
10492
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8(B).unwrap()`
@@ -107,16 +95,16 @@ LL | let _: &str = unsafe { std::mem::transmute(B) };
10795
= help: to override `-D warnings` add `#[allow(clippy::transmute_bytes_to_str)]`
10896

10997
error: transmute from a `&mut [u8]` to a `&mut str`
110-
--> tests/ui/transmute.rs:125:32
98+
--> tests/ui/transmute.rs:124:32
11199
|
112100
LL | let _: &mut str = unsafe { std::mem::transmute(mb) };
113101
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()`
114102

115103
error: transmute from a `&[u8]` to a `&str`
116-
--> tests/ui/transmute.rs:128:30
104+
--> tests/ui/transmute.rs:127:30
117105
|
118106
LL | const _: &str = unsafe { std::mem::transmute(B) };
119107
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_unchecked(B)`
120108

121-
error: aborting due to 18 previous errors
109+
error: aborting due to 16 previous errors
122110

src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.fixed

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ fn main() {
1313
// We should see an error message for each transmute, and no error messages for
1414
// the casts, since the casts are the recommended fixes.
1515

16-
// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
17-
let _ptr_i32_transmute = unsafe { usize::MAX as *const i32 };
18-
//~^ useless_transmute
1916
let ptr_i32 = usize::MAX as *const i32;
2017

2118
// e has type *T, U is *U_0, and either U_0: Sized ...

src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ fn main() {
1313
// We should see an error message for each transmute, and no error messages for
1414
// the casts, since the casts are the recommended fixes.
1515

16-
// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
17-
let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize::MAX) };
18-
//~^ useless_transmute
1916
let ptr_i32 = usize::MAX as *const i32;
2017

2118
// e has type *T, U is *U_0, and either U_0: Sized ...
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
error: transmute from an integer to a pointer
2-
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:17:39
3-
|
4-
LL | let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize::MAX) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `usize::MAX as *const i32`
6-
|
7-
= note: `-D clippy::useless-transmute` implied by `-D warnings`
8-
= help: to override `-D warnings` add `#[allow(clippy::useless_transmute)]`
9-
101
error: transmute from a pointer to a pointer
11-
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:22:38
2+
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:19:38
123
|
134
LL | let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr_i32) };
145
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -22,7 +13,7 @@ LL + let _ptr_i8_transmute = unsafe { ptr_i32.cast::<i8>() };
2213
|
2314

2415
error: transmute from a pointer to a pointer
25-
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:29:46
16+
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:26:46
2617
|
2718
LL | let _ptr_to_unsized_transmute = unsafe { transmute::<*const [i32], *const [u32]>(slice_ptr) };
2819
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -34,7 +25,7 @@ LL + let _ptr_to_unsized_transmute = unsafe { slice_ptr as *const [u32] };
3425
|
3526

3627
error: transmute from `*const i32` to `usize` which could be expressed as a pointer cast instead
37-
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:36:50
28+
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:33:50
3829
|
3930
LL | let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) };
4031
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize`
@@ -43,40 +34,43 @@ LL | let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, us
4334
= help: to override `-D warnings` add `#[allow(clippy::transmutes_expressible_as_ptr_casts)]`
4435

4536
error: transmute from a reference to a pointer
46-
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:43:41
37+
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:40:41
4738
|
4839
LL | let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; 4]>(array_ref) };
4940
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `array_ref as *const [i32; 4]`
41+
|
42+
= note: `-D clippy::useless-transmute` implied by `-D warnings`
43+
= help: to override `-D warnings` add `#[allow(clippy::useless_transmute)]`
5044

5145
error: transmute from `fn(usize) -> u8` to `*const usize` which could be expressed as a pointer cast instead
52-
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:52:41
46+
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:49:41
5347
|
5448
LL | let _usize_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, *const usize>(foo) };
5549
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize`
5650

5751
error: transmute from `fn(usize) -> u8` to `usize` which could be expressed as a pointer cast instead
58-
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:57:49
52+
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:54:49
5953
|
6054
LL | let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };
6155
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize`
6256

6357
error: transmute from `*const u32` to `usize` which could be expressed as a pointer cast instead
64-
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:61:36
58+
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:58:36
6559
|
6660
LL | let _usize_from_ref = unsafe { transmute::<*const u32, usize>(&1u32) };
6761
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&1u32 as *const u32 as usize`
6862

6963
error: transmute from a reference to a pointer
70-
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:73:14
64+
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:70:14
7165
|
7266
LL | unsafe { transmute::<&[i32; 1], *const u8>(in_param) }
7367
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `in_param as *const [i32; 1] as *const u8`
7468

7569
error: transmute from `fn()` to `*const u8` which could be expressed as a pointer cast instead
76-
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:92:28
70+
--> tests/ui/transmutes_expressible_as_ptr_casts.rs:89:28
7771
|
7872
LL | let _x: u8 = unsafe { *std::mem::transmute::<fn(), *const u8>(f) };
7973
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(f as *const u8)`
8074

81-
error: aborting due to 10 previous errors
75+
error: aborting due to 9 previous errors
8276

0 commit comments

Comments
 (0)