Skip to content

Commit 1f0a864

Browse files
committed
Suggest into instead of try_into if possible with int types
If it is possible to convert an integer type into another using `into`, don't suggest `try_into`. This commit changes the suggested method to convert from one integer type to another for the following cases: - u{n} -> i{m} where n < m - u8 -> isize - i{n} -> isize where n <= 16 - u{n} -> usize where n <= 16
1 parent 46ec74e commit 1f0a864

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

src/librustc_typeck/check/demand.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
767767
suggest_to_change_suffix_or_into(err, is_fallible);
768768
true
769769
}
770-
(&ty::Int(_), &ty::Uint(_)) | (&ty::Uint(_), &ty::Int(_)) => {
770+
(&ty::Int(exp), &ty::Uint(found)) => {
771+
let is_fallible = match (exp.bit_width(), found.bit_width()) {
772+
(Some(exp), Some(found)) if found < exp => false,
773+
(None, Some(found)) if found <= 16 => false,
774+
_ => true
775+
};
776+
suggest_to_change_suffix_or_into(err, is_fallible);
777+
true
778+
},
779+
(&ty::Uint(_), &ty::Int(_)) => {
771780
suggest_to_change_suffix_or_into(err, true);
772781
true
773782
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
fn main() {
2+
let a = 1u8;
3+
let _: i64 = a;
4+
//~^ ERROR mismatched types
5+
6+
let b = 1i8;
7+
let _: isize = b;
8+
//~^ ERROR mismatched types
9+
10+
let c = 1u8;
11+
let _: isize = c;
12+
//~^ ERROR mismatched types
13+
14+
let d = 1u8;
15+
let _: usize = d;
16+
//~^ ERROR mismatched types
17+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/integer-into.rs:3:18
3+
|
4+
LL | let _: i64 = a;
5+
| --- ^
6+
| | |
7+
| | expected `i64`, found `u8`
8+
| | help: you can convert an `u8` to `i64`: `a.into()`
9+
| expected due to this
10+
11+
error[E0308]: mismatched types
12+
--> $DIR/integer-into.rs:7:20
13+
|
14+
LL | let _: isize = b;
15+
| ----- ^
16+
| | |
17+
| | expected `isize`, found `i8`
18+
| | help: you can convert an `i8` to `isize`: `b.into()`
19+
| expected due to this
20+
21+
error[E0308]: mismatched types
22+
--> $DIR/integer-into.rs:11:20
23+
|
24+
LL | let _: isize = c;
25+
| ----- ^
26+
| | |
27+
| | expected `isize`, found `u8`
28+
| | help: you can convert an `u8` to `isize`: `c.into()`
29+
| expected due to this
30+
31+
error[E0308]: mismatched types
32+
--> $DIR/integer-into.rs:15:20
33+
|
34+
LL | let _: usize = d;
35+
| ----- ^
36+
| | |
37+
| | expected `usize`, found `u8`
38+
| | help: you can convert an `u8` to `usize`: `d.into()`
39+
| expected due to this
40+
41+
error: aborting due to 4 previous errors
42+
43+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)