Skip to content

Commit fb89ac6

Browse files
committed
add signed integers to unnecessary_lints to ensure feature parity with clippy
1 parent 7e552b4 commit fb89ac6

File tree

5 files changed

+128
-14
lines changed

5 files changed

+128
-14
lines changed

compiler/rustc_mir_transform/src/check_unnecessary_transmutes.rs

Lines changed: 27 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// We're testing x86 target specific features
22
//@only-target: x86_64 i686
33

4+
#![allow(unnecessary_transmutes)]
45
#[cfg(target_arch = "x86")]
56
use std::arch::x86::*;
67
#[cfg(target_arch = "x86_64")]

tests/ui/transmute/unnecessary-transmutation.fixed

Lines changed: 18 additions & 0 deletions
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,20 @@ 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+
84+
let y: f64 = f64::from_bits(1isize as _);
85+
//~^ ERROR
86+
let y: usize = f64::to_bits(1.0f64) as usize;
87+
//~^ ERROR
88+
let y: f64 = f64::from_bits(1usize as _);
89+
//~^ ERROR
90+
let y: isize = f64::to_bits(1.0f64) as isize;
91+
//~^ ERROR
92+
7593
let z: bool = (1u8 == 1);
7694
//~^ ERROR
7795
let z: u8 = (z) as u8;

tests/ui/transmute/unnecessary-transmutation.rs

Lines changed: 18 additions & 0 deletions
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,20 @@ 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+
84+
let y: f64 = transmute(1isize);
85+
//~^ ERROR
86+
let y: usize = transmute(1.0f64);
87+
//~^ ERROR
88+
let y: f64 = transmute(1usize);
89+
//~^ ERROR
90+
let y: isize = transmute(1.0f64);
91+
//~^ ERROR
92+
7593
let z: bool = transmute(1u8);
7694
//~^ ERROR
7795
let z: u8 = transmute(z);

tests/ui/transmute/unnecessary-transmutation.stderr

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,82 +154,132 @@ 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:22
244+
|
245+
LL | let y: f64 = transmute(1isize);
246+
| ^^^^^^^^^^^^^^^^^ help: replace this with: `f64::from_bits(1isize as _)`
247+
248+
error: unnecessary transmute
249+
--> $DIR/unnecessary-transmutation.rs:86:24
250+
|
251+
LL | let y: usize = transmute(1.0f64);
252+
| ^^^^^^^^^^^^^^^^^ help: replace this with: `f64::to_bits(1.0f64) as usize`
253+
254+
error: unnecessary transmute
255+
--> $DIR/unnecessary-transmutation.rs:88:22
256+
|
257+
LL | let y: f64 = transmute(1usize);
258+
| ^^^^^^^^^^^^^^^^^ help: replace this with: `f64::from_bits(1usize as _)`
259+
260+
error: unnecessary transmute
261+
--> $DIR/unnecessary-transmutation.rs:90:24
262+
|
263+
LL | let y: isize = transmute(1.0f64);
264+
| ^^^^^^^^^^^^^^^^^ help: replace this with: `f64::to_bits(1.0f64) as isize`
265+
266+
error: unnecessary transmute
267+
--> $DIR/unnecessary-transmutation.rs:93:23
218268
|
219269
LL | let z: bool = transmute(1u8);
220270
| ^^^^^^^^^^^^^^ help: replace this with: `(1u8 == 1)`
221271

222272
error: unnecessary transmute
223-
--> $DIR/unnecessary-transmutation.rs:77:21
273+
--> $DIR/unnecessary-transmutation.rs:95:21
224274
|
225275
LL | let z: u8 = transmute(z);
226276
| ^^^^^^^^^^^^ help: replace this with: `(z) as u8`
227277

228278
error: unnecessary transmute
229-
--> $DIR/unnecessary-transmutation.rs:82:21
279+
--> $DIR/unnecessary-transmutation.rs:100:21
230280
|
231281
LL | let z: i8 = transmute(z);
232282
| ^^^^^^^^^^^^ help: replace this with: `(z) as i8`
233283

234-
error: aborting due to 32 previous errors
284+
error: aborting due to 40 previous errors
235285

0 commit comments

Comments
 (0)