Skip to content

Commit 1acb537

Browse files
committed
Do not suggest .try_into() on i32::from(x)
1 parent 7b0085a commit 1acb537

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/librustc_typeck/check/demand.rs

+24
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,30 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
587587
return false;
588588
}
589589
}
590+
if let hir::ExprKind::Call(path, args) = &expr.node {
591+
if let (
592+
hir::ExprKind::Path(hir::QPath::TypeRelative(base_ty, path_segment)),
593+
1,
594+
) = (&path.node, args.len()) {
595+
// `expr` is a conversion like `u32::from(val)`, do not suggest anything (#63697).
596+
if let (
597+
hir::TyKind::Path(hir::QPath::Resolved(None, base_ty_path)),
598+
sym::from,
599+
) = (&base_ty.node, path_segment.ident.name) {
600+
if let Some(ident) = &base_ty_path.segments.iter().map(|s| s.ident).next() {
601+
match ident.name {
602+
sym::i128 | sym::i64 | sym::i32 | sym::i16 | sym::i8 |
603+
sym::u128 | sym::u64 | sym::u32 | sym::u16 | sym::u8 |
604+
sym::isize | sym::usize
605+
if base_ty_path.segments.len() == 1 => {
606+
return false;
607+
}
608+
_ => {}
609+
}
610+
}
611+
}
612+
}
613+
}
590614

591615
let msg = format!("you can convert an `{}` to `{}`", checked_ty, expected_ty);
592616
let cast_msg = format!("you can cast an `{} to `{}`", checked_ty, expected_ty);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
let _: u32 = i32::from(0_u8); //~ ERROR mismatched types
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/mismatched-types-numeric-from.rs:2:18
3+
|
4+
LL | let _: u32 = i32::from(0_u8);
5+
| ^^^^^^^^^^^^^^^ expected u32, found i32
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)