Skip to content

Commit 0c02f8a

Browse files
committed
fixup! Provide suggestion to convert numeric op LHS rather than unwrapping RHS
1 parent e243f62 commit 0c02f8a

File tree

3 files changed

+463
-138
lines changed

3 files changed

+463
-138
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
// run-rustfix
2+
3+
// The `try_into` suggestion doesn't include this, but we do suggest it after applying it
4+
use std::convert::TryInto;
5+
6+
#[allow(unused_must_use)]
7+
fn main() {
8+
let x_usize: usize = 1;
9+
let x_u128: u128 = 2;
10+
let x_u64: u64 = 3;
11+
let x_u32: u32 = 4;
12+
let x_u16: u16 = 5;
13+
let x_u8: u8 = 6;
14+
let x_isize: isize = 7;
15+
let x_i64: i64 = 8;
16+
let x_i32: i32 = 9;
17+
let x_i16: i16 = 10;
18+
let x_i8: i8 = 11;
19+
let x_i128: i128 = 12;
20+
21+
/* u<->u */
22+
{
23+
u16::from(x_u8) > x_u16;
24+
//~^ ERROR mismatched types
25+
u32::from(x_u8) > x_u32;
26+
//~^ ERROR mismatched types
27+
u64::from(x_u8) > x_u64;
28+
//~^ ERROR mismatched types
29+
u128::from(x_u8) > x_u128;
30+
//~^ ERROR mismatched types
31+
usize::from(x_u8) > x_usize;
32+
//~^ ERROR mismatched types
33+
34+
x_u16 > x_u8.into();
35+
//~^ ERROR mismatched types
36+
u32::from(x_u16) > x_u32;
37+
//~^ ERROR mismatched types
38+
u64::from(x_u16) > x_u64;
39+
//~^ ERROR mismatched types
40+
u128::from(x_u16) > x_u128;
41+
//~^ ERROR mismatched types
42+
usize::from(x_u16) > x_usize;
43+
//~^ ERROR mismatched types
44+
45+
x_u32 > x_u8.into();
46+
//~^ ERROR mismatched types
47+
x_u32 > x_u16.into();
48+
//~^ ERROR mismatched types
49+
u64::from(x_u32) > x_u64;
50+
//~^ ERROR mismatched types
51+
u128::from(x_u32) > x_u128;
52+
//~^ ERROR mismatched types
53+
x_u32 > x_usize.try_into().unwrap();
54+
//~^ ERROR mismatched types
55+
56+
x_u64 > x_u8.into();
57+
//~^ ERROR mismatched types
58+
x_u64 > x_u16.into();
59+
//~^ ERROR mismatched types
60+
x_u64 > x_u32.into();
61+
//~^ ERROR mismatched types
62+
u128::from(x_u64) > x_u128;
63+
//~^ ERROR mismatched types
64+
x_u64 > x_usize.try_into().unwrap();
65+
//~^ ERROR mismatched types
66+
67+
x_u128 > x_u8.into();
68+
//~^ ERROR mismatched types
69+
x_u128 > x_u16.into();
70+
//~^ ERROR mismatched types
71+
x_u128 > x_u32.into();
72+
//~^ ERROR mismatched types
73+
x_u128 > x_u64.into();
74+
//~^ ERROR mismatched types
75+
x_u128 > x_usize.try_into().unwrap();
76+
//~^ ERROR mismatched types
77+
78+
x_usize > x_u8.into();
79+
//~^ ERROR mismatched types
80+
x_usize > x_u16.into();
81+
//~^ ERROR mismatched types
82+
x_usize > x_u32.try_into().unwrap();
83+
//~^ ERROR mismatched types
84+
x_usize > x_u64.try_into().unwrap();
85+
//~^ ERROR mismatched types
86+
x_usize > x_u128.try_into().unwrap();
87+
//~^ ERROR mismatched types
88+
}
89+
90+
/* i<->i */
91+
{
92+
i16::from(x_i8) > x_i16;
93+
//~^ ERROR mismatched types
94+
i32::from(x_i8) > x_i32;
95+
//~^ ERROR mismatched types
96+
i64::from(x_i8) > x_i64;
97+
//~^ ERROR mismatched types
98+
i128::from(x_i8) > x_i128;
99+
//~^ ERROR mismatched types
100+
isize::from(x_i8) > x_isize;
101+
//~^ ERROR mismatched types
102+
103+
x_i16 > x_i8.into();
104+
//~^ ERROR mismatched types
105+
i32::from(x_i16) > x_i32;
106+
//~^ ERROR mismatched types
107+
i64::from(x_i16) > x_i64;
108+
//~^ ERROR mismatched types
109+
i128::from(x_i16) > x_i128;
110+
//~^ ERROR mismatched types
111+
isize::from(x_i16) > x_isize;
112+
//~^ ERROR mismatched types
113+
114+
x_i32 > x_i8.into();
115+
//~^ ERROR mismatched types
116+
x_i32 > x_i16.into();
117+
//~^ ERROR mismatched types
118+
i64::from(x_i32) > x_i64;
119+
//~^ ERROR mismatched types
120+
i128::from(x_i32) > x_i128;
121+
//~^ ERROR mismatched types
122+
x_i32 > x_isize.try_into().unwrap();
123+
//~^ ERROR mismatched types
124+
125+
x_i64 > x_i8.into();
126+
//~^ ERROR mismatched types
127+
x_i64 > x_i16.into();
128+
//~^ ERROR mismatched types
129+
x_i64 > x_i32.into();
130+
//~^ ERROR mismatched types
131+
i128::from(x_i64) > x_i128;
132+
//~^ ERROR mismatched types
133+
x_i64 > x_isize.try_into().unwrap();
134+
//~^ ERROR mismatched types
135+
136+
x_i128 > x_i8.into();
137+
//~^ ERROR mismatched types
138+
x_i128 > x_i16.into();
139+
//~^ ERROR mismatched types
140+
x_i128 > x_i32.into();
141+
//~^ ERROR mismatched types
142+
x_i128 > x_i64.into();
143+
//~^ ERROR mismatched types
144+
x_i128 > x_isize.try_into().unwrap();
145+
//~^ ERROR mismatched types
146+
147+
x_isize > x_i8.into();
148+
//~^ ERROR mismatched types
149+
x_isize > x_i16.into();
150+
//~^ ERROR mismatched types
151+
x_isize > x_i32.try_into().unwrap();
152+
//~^ ERROR mismatched types
153+
x_isize > x_i64.try_into().unwrap();
154+
//~^ ERROR mismatched types
155+
x_isize > x_i128.try_into().unwrap();
156+
//~^ ERROR mismatched types
157+
}
158+
159+
/* u<->i */
160+
{
161+
x_u8 > x_i8.try_into().unwrap();
162+
//~^ ERROR mismatched types
163+
i16::from(x_u8) > x_i16;
164+
//~^ ERROR mismatched types
165+
i32::from(x_u8) > x_i32;
166+
//~^ ERROR mismatched types
167+
i64::from(x_u8) > x_i64;
168+
//~^ ERROR mismatched types
169+
i128::from(x_u8) > x_i128;
170+
//~^ ERROR mismatched types
171+
isize::from(x_u8) > x_isize;
172+
//~^ ERROR mismatched types
173+
174+
x_u16 > x_i8.try_into().unwrap();
175+
//~^ ERROR mismatched types
176+
x_u16 > x_i16.try_into().unwrap();
177+
//~^ ERROR mismatched types
178+
i32::from(x_u16) > x_i32;
179+
//~^ ERROR mismatched types
180+
i64::from(x_u16) > x_i64;
181+
//~^ ERROR mismatched types
182+
i128::from(x_u16) > x_i128;
183+
//~^ ERROR mismatched types
184+
x_u16 > x_isize.try_into().unwrap();
185+
//~^ ERROR mismatched types
186+
187+
x_u32 > x_i8.try_into().unwrap();
188+
//~^ ERROR mismatched types
189+
x_u32 > x_i16.try_into().unwrap();
190+
//~^ ERROR mismatched types
191+
x_u32 > x_i32.try_into().unwrap();
192+
//~^ ERROR mismatched types
193+
i64::from(x_u32) > x_i64;
194+
//~^ ERROR mismatched types
195+
i128::from(x_u32) > x_i128;
196+
//~^ ERROR mismatched types
197+
x_u32 > x_isize.try_into().unwrap();
198+
//~^ ERROR mismatched types
199+
200+
x_u64 > x_i8.try_into().unwrap();
201+
//~^ ERROR mismatched types
202+
x_u64 > x_i16.try_into().unwrap();
203+
//~^ ERROR mismatched types
204+
x_u64 > x_i32.try_into().unwrap();
205+
//~^ ERROR mismatched types
206+
x_u64 > x_i64.try_into().unwrap();
207+
//~^ ERROR mismatched types
208+
i128::from(x_u64) > x_i128;
209+
//~^ ERROR mismatched types
210+
x_u64 > x_isize.try_into().unwrap();
211+
//~^ ERROR mismatched types
212+
213+
x_u128 > x_i8.try_into().unwrap();
214+
//~^ ERROR mismatched types
215+
x_u128 > x_i16.try_into().unwrap();
216+
//~^ ERROR mismatched types
217+
x_u128 > x_i32.try_into().unwrap();
218+
//~^ ERROR mismatched types
219+
x_u128 > x_i64.try_into().unwrap();
220+
//~^ ERROR mismatched types
221+
x_u128 > x_i128.try_into().unwrap();
222+
//~^ ERROR mismatched types
223+
x_u128 > x_isize.try_into().unwrap();
224+
//~^ ERROR mismatched types
225+
226+
x_usize > x_i8.try_into().unwrap();
227+
//~^ ERROR mismatched types
228+
x_usize > x_i16.try_into().unwrap();
229+
//~^ ERROR mismatched types
230+
x_usize > x_i32.try_into().unwrap();
231+
//~^ ERROR mismatched types
232+
x_usize > x_i64.try_into().unwrap();
233+
//~^ ERROR mismatched types
234+
x_usize > x_i128.try_into().unwrap();
235+
//~^ ERROR mismatched types
236+
x_usize > x_isize.try_into().unwrap();
237+
//~^ ERROR mismatched types
238+
}
239+
240+
/* i<->u */
241+
{
242+
x_i8 > x_u8.try_into().unwrap();
243+
//~^ ERROR mismatched types
244+
x_i8 > x_u16.try_into().unwrap();
245+
//~^ ERROR mismatched types
246+
x_i8 > x_u32.try_into().unwrap();
247+
//~^ ERROR mismatched types
248+
x_i8 > x_u64.try_into().unwrap();
249+
//~^ ERROR mismatched types
250+
x_i8 > x_u128.try_into().unwrap();
251+
//~^ ERROR mismatched types
252+
x_i8 > x_usize.try_into().unwrap();
253+
//~^ ERROR mismatched types
254+
255+
x_i16 > x_u8.into();
256+
//~^ ERROR mismatched types
257+
x_i16 > x_u16.try_into().unwrap();
258+
//~^ ERROR mismatched types
259+
x_i16 > x_u32.try_into().unwrap();
260+
//~^ ERROR mismatched types
261+
x_i16 > x_u64.try_into().unwrap();
262+
//~^ ERROR mismatched types
263+
x_i16 > x_u128.try_into().unwrap();
264+
//~^ ERROR mismatched types
265+
x_i16 > x_usize.try_into().unwrap();
266+
//~^ ERROR mismatched types
267+
268+
x_i32 > x_u8.into();
269+
//~^ ERROR mismatched types
270+
x_i32 > x_u16.into();
271+
//~^ ERROR mismatched types
272+
x_i32 > x_u32.try_into().unwrap();
273+
//~^ ERROR mismatched types
274+
x_i32 > x_u64.try_into().unwrap();
275+
//~^ ERROR mismatched types
276+
x_i32 > x_u128.try_into().unwrap();
277+
//~^ ERROR mismatched types
278+
x_i32 > x_usize.try_into().unwrap();
279+
//~^ ERROR mismatched types
280+
281+
x_i64 > x_u8.into();
282+
//~^ ERROR mismatched types
283+
x_i64 > x_u16.into();
284+
//~^ ERROR mismatched types
285+
x_i64 > x_u32.into();
286+
//~^ ERROR mismatched types
287+
x_i64 > x_u64.try_into().unwrap();
288+
//~^ ERROR mismatched types
289+
x_i64 > x_u128.try_into().unwrap();
290+
//~^ ERROR mismatched types
291+
x_i64 > x_usize.try_into().unwrap();
292+
//~^ ERROR mismatched types
293+
294+
x_i128 > x_u8.into();
295+
//~^ ERROR mismatched types
296+
x_i128 > x_u16.into();
297+
//~^ ERROR mismatched types
298+
x_i128 > x_u32.into();
299+
//~^ ERROR mismatched types
300+
x_i128 > x_u64.into();
301+
//~^ ERROR mismatched types
302+
x_i128 > x_u128.try_into().unwrap();
303+
//~^ ERROR mismatched types
304+
x_i128 > x_usize.try_into().unwrap();
305+
//~^ ERROR mismatched types
306+
307+
x_isize > x_u8.into();
308+
//~^ ERROR mismatched types
309+
x_isize > x_u16.try_into().unwrap();
310+
//~^ ERROR mismatched types
311+
x_isize > x_u32.try_into().unwrap();
312+
//~^ ERROR mismatched types
313+
x_isize > x_u64.try_into().unwrap();
314+
//~^ ERROR mismatched types
315+
x_isize > x_u128.try_into().unwrap();
316+
//~^ ERROR mismatched types
317+
x_isize > x_usize.try_into().unwrap();
318+
//~^ ERROR mismatched types
319+
}
320+
}

src/test/ui/numeric/numeric-cast-binop.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// run-rustfix
2+
3+
// The `try_into` suggestion doesn't include this, but we do suggest it after applying it
4+
use std::convert::TryInto;
5+
6+
#[allow(unused_must_use)]
17
fn main() {
28
let x_usize: usize = 1;
39
let x_u128: u128 = 2;
@@ -6,12 +12,11 @@ fn main() {
612
let x_u16: u16 = 5;
713
let x_u8: u8 = 6;
814
let x_isize: isize = 7;
9-
let x_i128: i128 = 8;
10-
let x_i64: i64 = 9;
11-
let x_i32: i32 = 10;
12-
let x_i16: i16 = 11;
13-
let x_i8: i8 = 12;
14-
let x_i128: i128 = 13;
15+
let x_i64: i64 = 8;
16+
let x_i32: i32 = 9;
17+
let x_i16: i16 = 10;
18+
let x_i8: i8 = 11;
19+
let x_i128: i128 = 12;
1520

1621
/* u<->u */
1722
{

0 commit comments

Comments
 (0)