Skip to content

Commit e31493b

Browse files
Rollup merge of rust-lang#135003 - RalfJung:deprecate-allowed-through-unstable, r=davidtwco
deprecate `std::intrinsics::transmute` etc, use `std::mem::*` instead The `rustc_allowed_through_unstable_modules` attribute lets users call `std::mem::transmute` as `std::intrinsics::transmute`. The former is a reexport of the latter, and for a long time we didn't properly check stability for reexports, so making this a hard error now would be a breaking change for little gain. But at the same time, `std::intrinsics::transmute` is not the intended path for this function, so I think it is a good idea to show a deprecation warning when that path is used. This PR implements that, for all the functions in `std::intrinsics` that carry the attribute. I assume this will need ``@rust-lang/libs-api`` FCP.
2 parents f6df266 + caebd67 commit e31493b

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

clippy_lints/src/std_instead_of_core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ fn is_stable(cx: &LateContext<'_>, mut def_id: DefId, msrv: &Msrv) -> bool {
180180
if let Some(stability) = cx.tcx.lookup_stability(def_id)
181181
&& let StabilityLevel::Stable {
182182
since,
183-
allowed_through_unstable_modules: false,
183+
allowed_through_unstable_modules: None,
184184
} = stability.level
185185
{
186186
let stable = match since {

tests/ui/std_instead_of_core.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@aux-build:proc_macro_derive.rs
22

33
#![warn(clippy::std_instead_of_core)]
4-
#![allow(unused_imports)]
4+
#![allow(unused_imports, deprecated)]
55

66
extern crate alloc;
77

tests/ui/std_instead_of_core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@aux-build:proc_macro_derive.rs
22

33
#![warn(clippy::std_instead_of_core)]
4-
#![allow(unused_imports)]
4+
#![allow(unused_imports, deprecated)]
55

66
extern crate alloc;
77

tests/ui/transmute.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,31 @@ fn my_vec() -> MyVec<i32> {
2424
#[warn(clippy::useless_transmute)]
2525
unsafe fn _generic<'a, T, U: 'a>(t: &'a T) {
2626
// FIXME: should lint
27-
// let _: &'a T = core::intrinsics::transmute(t);
27+
// let _: &'a T = core::mem::transmute(t);
2828

29-
let _: &'a U = core::intrinsics::transmute(t);
29+
let _: &'a U = core::mem::transmute(t);
3030

31-
let _: *const T = core::intrinsics::transmute(t);
31+
let _: *const T = core::mem::transmute(t);
3232
//~^ ERROR: transmute from a reference to a pointer
3333
//~| NOTE: `-D clippy::useless-transmute` implied by `-D warnings`
3434

35-
let _: *mut T = core::intrinsics::transmute(t);
35+
let _: *mut T = core::mem::transmute(t);
3636
//~^ ERROR: transmute from a reference to a pointer
3737

38-
let _: *const U = core::intrinsics::transmute(t);
38+
let _: *const U = core::mem::transmute(t);
3939
//~^ ERROR: transmute from a reference to a pointer
4040
}
4141

4242
#[warn(clippy::useless_transmute)]
4343
fn useless() {
4444
unsafe {
45-
let _: Vec<i32> = core::intrinsics::transmute(my_vec());
45+
let _: Vec<i32> = core::mem::transmute(my_vec());
4646
//~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself
4747

4848
let _: Vec<i32> = core::mem::transmute(my_vec());
4949
//~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself
5050

51-
let _: Vec<i32> = std::intrinsics::transmute(my_vec());
51+
let _: Vec<i32> = std::mem::transmute(my_vec());
5252
//~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself
5353

5454
let _: Vec<i32> = std::mem::transmute(my_vec());
@@ -94,17 +94,17 @@ fn crosspointer() {
9494
let int_mut_ptr: *mut Usize = &mut int as *mut Usize;
9595

9696
unsafe {
97-
let _: Usize = core::intrinsics::transmute(int_const_ptr);
97+
let _: Usize = core::mem::transmute(int_const_ptr);
9898
//~^ ERROR: transmute from a type (`*const Usize`) to the type that it points to (
9999
//~| NOTE: `-D clippy::crosspointer-transmute` implied by `-D warnings`
100100

101-
let _: Usize = core::intrinsics::transmute(int_mut_ptr);
101+
let _: Usize = core::mem::transmute(int_mut_ptr);
102102
//~^ ERROR: transmute from a type (`*mut Usize`) to the type that it points to (`U
103103

104-
let _: *const Usize = core::intrinsics::transmute(my_int());
104+
let _: *const Usize = core::mem::transmute(my_int());
105105
//~^ ERROR: transmute from a type (`Usize`) to a pointer to that type (`*const Usi
106106

107-
let _: *mut Usize = core::intrinsics::transmute(my_int());
107+
let _: *mut Usize = core::mem::transmute(my_int());
108108
//~^ ERROR: transmute from a type (`Usize`) to a pointer to that type (`*mut Usize
109109
}
110110
}

tests/ui/transmute.stderr

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
error: transmute from a reference to a pointer
22
--> tests/ui/transmute.rs:31:23
33
|
4-
LL | let _: *const T = core::intrinsics::transmute(t);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T`
4+
LL | let _: *const T = core::mem::transmute(t);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T`
66
|
77
= note: `-D clippy::useless-transmute` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::useless_transmute)]`
99

1010
error: transmute from a reference to a pointer
1111
--> tests/ui/transmute.rs:35:21
1212
|
13-
LL | let _: *mut T = core::intrinsics::transmute(t);
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T as *mut T`
13+
LL | let _: *mut T = core::mem::transmute(t);
14+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T as *mut T`
1515

1616
error: transmute from a reference to a pointer
1717
--> tests/ui/transmute.rs:38:23
1818
|
19-
LL | let _: *const U = core::intrinsics::transmute(t);
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T as *const U`
19+
LL | let _: *const U = core::mem::transmute(t);
20+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t as *const T as *const U`
2121

2222
error: transmute from a type (`std::vec::Vec<i32>`) to itself
2323
--> tests/ui/transmute.rs:45:27
2424
|
25-
LL | let _: Vec<i32> = core::intrinsics::transmute(my_vec());
26-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
LL | let _: Vec<i32> = core::mem::transmute(my_vec());
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727

2828
error: transmute from a type (`std::vec::Vec<i32>`) to itself
2929
--> tests/ui/transmute.rs:48:27
@@ -34,8 +34,8 @@ LL | let _: Vec<i32> = core::mem::transmute(my_vec());
3434
error: transmute from a type (`std::vec::Vec<i32>`) to itself
3535
--> tests/ui/transmute.rs:51:27
3636
|
37-
LL | let _: Vec<i32> = std::intrinsics::transmute(my_vec());
38-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37+
LL | let _: Vec<i32> = std::mem::transmute(my_vec());
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3939

4040
error: transmute from a type (`std::vec::Vec<i32>`) to itself
4141
--> tests/ui/transmute.rs:54:27
@@ -64,29 +64,29 @@ LL | let _: *const usize = std::mem::transmute(1 + 1usize);
6464
error: transmute from a type (`*const Usize`) to the type that it points to (`Usize`)
6565
--> tests/ui/transmute.rs:97:24
6666
|
67-
LL | let _: Usize = core::intrinsics::transmute(int_const_ptr);
68-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
LL | let _: Usize = core::mem::transmute(int_const_ptr);
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6969
|
7070
= note: `-D clippy::crosspointer-transmute` implied by `-D warnings`
7171
= help: to override `-D warnings` add `#[allow(clippy::crosspointer_transmute)]`
7272

7373
error: transmute from a type (`*mut Usize`) to the type that it points to (`Usize`)
7474
--> tests/ui/transmute.rs:101:24
7575
|
76-
LL | let _: Usize = core::intrinsics::transmute(int_mut_ptr);
77-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
76+
LL | let _: Usize = core::mem::transmute(int_mut_ptr);
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7878

7979
error: transmute from a type (`Usize`) to a pointer to that type (`*const Usize`)
8080
--> tests/ui/transmute.rs:104:31
8181
|
82-
LL | let _: *const Usize = core::intrinsics::transmute(my_int());
83-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
82+
LL | let _: *const Usize = core::mem::transmute(my_int());
83+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8484

8585
error: transmute from a type (`Usize`) to a pointer to that type (`*mut Usize`)
8686
--> tests/ui/transmute.rs:107:29
8787
|
88-
LL | let _: *mut Usize = core::intrinsics::transmute(my_int());
89-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88+
LL | let _: *mut Usize = core::mem::transmute(my_int());
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9090

9191
error: transmute from a `u8` to a `bool`
9292
--> tests/ui/transmute.rs:114:28

0 commit comments

Comments
 (0)