Skip to content

Commit 16ca18d

Browse files
authored
Rollup merge of #140804 - bend-n:signed, r=lcnr
add signed ints to unn- transmutes to ensure feature parity i forgot a few cases rust-lang/rust-clippy#14703 adds - char -> i32 - i32 -> char - float -> size () - size -> float - i32 -> float ``@rustbot`` label L-unnecessary_transmutes
2 parents b165a4c + 3b4c493 commit 16ca18d

File tree

5 files changed

+86
-14
lines changed

5 files changed

+86
-14
lines changed

compiler/rustc_mir_transform/src/check_unnecessary_transmutes.rs

+27
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,45 @@ impl<'a, 'tcx> UnnecessaryTransmuteChecker<'a, 'tcx> {
5555
},
5656
// char → u32
5757
(Char, Uint(UintTy::U32)) => err(format!("u32::from({arg})")),
58+
// char (→ u32) → i32
59+
(Char, Int(IntTy::I32)) => err(format!("u32::from({arg}).cast_signed()")),
5860
// u32 → char
5961
(Uint(UintTy::U32), Char) => Error {
6062
sugg: format!("char::from_u32_unchecked({arg})"),
6163
help: Some("consider `char::from_u32(…).unwrap()`"),
6264
span,
6365
},
66+
// i32 → char
67+
(Int(IntTy::I32), Char) => Error {
68+
sugg: format!("char::from_u32_unchecked(i32::cast_unsigned({arg}))"),
69+
help: Some("consider `char::from_u32(i32::cast_unsigned(…)).unwrap()`"),
70+
span,
71+
},
6472
// uNN → iNN
6573
(Uint(ty), Int(_)) => err(format!("{}::cast_signed({arg})", ty.name_str())),
6674
// iNN → uNN
6775
(Int(ty), Uint(_)) => err(format!("{}::cast_unsigned({arg})", ty.name_str())),
76+
// fNN → xsize
77+
(Float(ty), Uint(UintTy::Usize)) => {
78+
err(format!("{}::to_bits({arg}) as usize", ty.name_str()))
79+
}
80+
(Float(ty), Int(IntTy::Isize)) => {
81+
err(format!("{}::to_bits({arg}) as isize", ty.name_str()))
82+
}
83+
// fNN (→ uNN) → iNN
84+
(Float(ty), Int(..)) => err(format!("{}::to_bits({arg}).cast_signed()", ty.name_str())),
6885
// fNN → uNN
6986
(Float(ty), Uint(..)) => err(format!("{}::to_bits({arg})", ty.name_str())),
87+
// xsize → fNN
88+
(Uint(UintTy::Usize) | Int(IntTy::Isize), Float(ty)) => {
89+
err(format!("{}::from_bits({arg} as _)", ty.name_str(),))
90+
}
91+
// iNN (→ uNN) → fNN
92+
(Int(int_ty), Float(ty)) => err(format!(
93+
"{}::from_bits({}::cast_unsigned({arg}))",
94+
ty.name_str(),
95+
int_ty.name_str()
96+
)),
7097
// uNN → fNN
7198
(Uint(_), Float(ty)) => err(format!("{}::from_bits({arg})", ty.name_str())),
7299
// bool → { x8 }

src/tools/miri/tests/pass/shims/x86/intrinsics-x86-sse2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// We're testing x86 target specific features
22
//@only-target: x86_64 i686
3+
#![allow(unnecessary_transmutes)]
34

45
#[cfg(target_arch = "x86")]
56
use std::arch::x86::*;

tests/ui/transmute/unnecessary-transmutation.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ fn main() {
4949
//~^ ERROR
5050
let y: char = char::from_u32_unchecked(y);
5151
//~^ ERROR
52+
let y: i32 = u32::from('🐱').cast_signed();
53+
//~^ ERROR
54+
let y: char = char::from_u32_unchecked(i32::cast_unsigned(y));
55+
//~^ ERROR
5256

5357
let x: u16 = i16::cast_unsigned(8i16);
5458
//~^ ERROR
@@ -72,6 +76,11 @@ fn main() {
7276
let y: u64 = f64::to_bits(2.0);
7377
//~^ ERROR
7478

79+
let y: f64 = f64::from_bits(i64::cast_unsigned(1i64));
80+
//~^ ERROR
81+
let y: i64 = f64::to_bits(1f64).cast_signed();
82+
//~^ ERROR
83+
7584
let z: bool = (1u8 == 1);
7685
//~^ ERROR
7786
let z: u8 = (z) as u8;

tests/ui/transmute/unnecessary-transmutation.rs

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ fn main() {
4949
//~^ ERROR
5050
let y: char = transmute(y);
5151
//~^ ERROR
52+
let y: i32 = transmute('🐱');
53+
//~^ ERROR
54+
let y: char = transmute(y);
55+
//~^ ERROR
5256

5357
let x: u16 = transmute(8i16);
5458
//~^ ERROR
@@ -72,6 +76,11 @@ fn main() {
7276
let y: u64 = transmute(2.0);
7377
//~^ ERROR
7478

79+
let y: f64 = transmute(1i64);
80+
//~^ ERROR
81+
let y: i64 = transmute(1f64);
82+
//~^ ERROR
83+
7584
let z: bool = transmute(1u8);
7685
//~^ ERROR
7786
let z: u8 = transmute(z);

tests/ui/transmute/unnecessary-transmutation.stderr

+40-14
Original file line numberDiff line numberDiff line change
@@ -154,82 +154,108 @@ LL | let y: char = transmute(y);
154154
= help: consider `char::from_u32(…).unwrap()`
155155

156156
error: unnecessary transmute
157-
--> $DIR/unnecessary-transmutation.rs:53:22
157+
--> $DIR/unnecessary-transmutation.rs:52:22
158+
|
159+
LL | let y: i32 = transmute('🐱');
160+
| ^^^^^^^^^^^^^^^ help: replace this with: `u32::from('🐱').cast_signed()`
161+
162+
error: unnecessary transmute
163+
--> $DIR/unnecessary-transmutation.rs:54:23
164+
|
165+
LL | let y: char = transmute(y);
166+
| ^^^^^^^^^^^^ help: replace this with: `char::from_u32_unchecked(i32::cast_unsigned(y))`
167+
|
168+
= help: consider `char::from_u32(i32::cast_unsigned(…)).unwrap()`
169+
170+
error: unnecessary transmute
171+
--> $DIR/unnecessary-transmutation.rs:57:22
158172
|
159173
LL | let x: u16 = transmute(8i16);
160174
| ^^^^^^^^^^^^^^^ help: replace this with: `i16::cast_unsigned(8i16)`
161175

162176
error: unnecessary transmute
163-
--> $DIR/unnecessary-transmutation.rs:55:22
177+
--> $DIR/unnecessary-transmutation.rs:59:22
164178
|
165179
LL | let x: i16 = transmute(x);
166180
| ^^^^^^^^^^^^ help: replace this with: `u16::cast_signed(x)`
167181

168182
error: unnecessary transmute
169-
--> $DIR/unnecessary-transmutation.rs:57:22
183+
--> $DIR/unnecessary-transmutation.rs:61:22
170184
|
171185
LL | let x: u32 = transmute(4i32);
172186
| ^^^^^^^^^^^^^^^ help: replace this with: `i32::cast_unsigned(4i32)`
173187

174188
error: unnecessary transmute
175-
--> $DIR/unnecessary-transmutation.rs:59:22
189+
--> $DIR/unnecessary-transmutation.rs:63:22
176190
|
177191
LL | let x: i32 = transmute(x);
178192
| ^^^^^^^^^^^^ help: replace this with: `u32::cast_signed(x)`
179193

180194
error: unnecessary transmute
181-
--> $DIR/unnecessary-transmutation.rs:61:22
195+
--> $DIR/unnecessary-transmutation.rs:65:22
182196
|
183197
LL | let x: u64 = transmute(7i64);
184198
| ^^^^^^^^^^^^^^^ help: replace this with: `i64::cast_unsigned(7i64)`
185199

186200
error: unnecessary transmute
187-
--> $DIR/unnecessary-transmutation.rs:63:22
201+
--> $DIR/unnecessary-transmutation.rs:67:22
188202
|
189203
LL | let x: i64 = transmute(x);
190204
| ^^^^^^^^^^^^ help: replace this with: `u64::cast_signed(x)`
191205

192206
error: unnecessary transmute
193-
--> $DIR/unnecessary-transmutation.rs:66:22
207+
--> $DIR/unnecessary-transmutation.rs:70:22
194208
|
195209
LL | let y: f32 = transmute(1u32);
196210
| ^^^^^^^^^^^^^^^ help: replace this with: `f32::from_bits(1u32)`
197211

198212
error: unnecessary transmute
199-
--> $DIR/unnecessary-transmutation.rs:68:22
213+
--> $DIR/unnecessary-transmutation.rs:72:22
200214
|
201215
LL | let y: u32 = transmute(y);
202216
| ^^^^^^^^^^^^ help: replace this with: `f32::to_bits(y)`
203217

204218
error: unnecessary transmute
205-
--> $DIR/unnecessary-transmutation.rs:70:22
219+
--> $DIR/unnecessary-transmutation.rs:74:22
206220
|
207221
LL | let y: f64 = transmute(3u64);
208222
| ^^^^^^^^^^^^^^^ help: replace this with: `f64::from_bits(3u64)`
209223

210224
error: unnecessary transmute
211-
--> $DIR/unnecessary-transmutation.rs:72:22
225+
--> $DIR/unnecessary-transmutation.rs:76:22
212226
|
213227
LL | let y: u64 = transmute(2.0);
214228
| ^^^^^^^^^^^^^^ help: replace this with: `f64::to_bits(2.0)`
215229

216230
error: unnecessary transmute
217-
--> $DIR/unnecessary-transmutation.rs:75:23
231+
--> $DIR/unnecessary-transmutation.rs:79:22
232+
|
233+
LL | let y: f64 = transmute(1i64);
234+
| ^^^^^^^^^^^^^^^ help: replace this with: `f64::from_bits(i64::cast_unsigned(1i64))`
235+
236+
error: unnecessary transmute
237+
--> $DIR/unnecessary-transmutation.rs:81:22
238+
|
239+
LL | let y: i64 = transmute(1f64);
240+
| ^^^^^^^^^^^^^^^ help: replace this with: `f64::to_bits(1f64).cast_signed()`
241+
242+
error: unnecessary transmute
243+
--> $DIR/unnecessary-transmutation.rs:84:23
218244
|
219245
LL | let z: bool = transmute(1u8);
220246
| ^^^^^^^^^^^^^^ help: replace this with: `(1u8 == 1)`
221247

222248
error: unnecessary transmute
223-
--> $DIR/unnecessary-transmutation.rs:77:21
249+
--> $DIR/unnecessary-transmutation.rs:86:21
224250
|
225251
LL | let z: u8 = transmute(z);
226252
| ^^^^^^^^^^^^ help: replace this with: `(z) as u8`
227253

228254
error: unnecessary transmute
229-
--> $DIR/unnecessary-transmutation.rs:82:21
255+
--> $DIR/unnecessary-transmutation.rs:91:21
230256
|
231257
LL | let z: i8 = transmute(z);
232258
| ^^^^^^^^^^^^ help: replace this with: `(z) as i8`
233259

234-
error: aborting due to 32 previous errors
260+
error: aborting due to 36 previous errors
235261

0 commit comments

Comments
 (0)