mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-03 20:23:59 +00:00
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:
commit
ed8e13c2cb
@ -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);
|
||||
|
3
src/test/ui/suggestions/mismatched-types-numeric-from.rs
Normal file
3
src/test/ui/suggestions/mismatched-types-numeric-from.rs
Normal file
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
let _: u32 = i32::from(0_u8); //~ ERROR mismatched types
|
||||
}
|
@ -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`.
|
Loading…
Reference in New Issue
Block a user