Rollup merge of #63813 - estebank:int-from, r=varkor

Do not suggest `.try_into()` on `i32::from(x)`

Fix #63697.
This commit is contained in:
Mazdak Farrokhzad 2019-08-25 02:44:59 +02:00 committed by GitHub
commit ed8e13c2cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 0 deletions

View File

@ -587,6 +587,30 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return false;
}
}
if let hir::ExprKind::Call(path, args) = &expr.node {
if let (
hir::ExprKind::Path(hir::QPath::TypeRelative(base_ty, path_segment)),
1,
) = (&path.node, args.len()) {
// `expr` is a conversion like `u32::from(val)`, do not suggest anything (#63697).
if let (
hir::TyKind::Path(hir::QPath::Resolved(None, base_ty_path)),
sym::from,
) = (&base_ty.node, path_segment.ident.name) {
if let Some(ident) = &base_ty_path.segments.iter().map(|s| s.ident).next() {
match ident.name {
sym::i128 | sym::i64 | sym::i32 | sym::i16 | sym::i8 |
sym::u128 | sym::u64 | sym::u32 | sym::u16 | sym::u8 |
sym::isize | sym::usize
if base_ty_path.segments.len() == 1 => {
return false;
}
_ => {}
}
}
}
}
}
let msg = format!("you can convert an `{}` to `{}`", checked_ty, expected_ty);
let cast_msg = format!("you can cast an `{} to `{}`", checked_ty, expected_ty);

View File

@ -0,0 +1,3 @@
fn main() {
let _: u32 = i32::from(0_u8); //~ ERROR mismatched types
}

View File

@ -0,0 +1,9 @@
error[E0308]: mismatched types
--> $DIR/mismatched-types-numeric-from.rs:2:18
|
LL | let _: u32 = i32::from(0_u8);
| ^^^^^^^^^^^^^^^ expected u32, found i32
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.