Rollup merge of #73195 - ayazhafiz:i/73145, r=estebank

Provide suggestion to convert numeric op LHS rather than unwrapping RHS

Given a code

```rust
fn foo(x: u8, y: u32) -> bool {
    x > y
}
fn main() {}
```

it could be more helpful to provide a suggestion to do "u32::from(x)"
rather than "y.try_into().unwrap()", since the latter may panic.

We do this by passing the LHS of a binary expression up the stack into
the coercion checker.

Closes #73145
This commit is contained in:
Dylan DPC 2020-06-12 00:05:33 +02:00 committed by GitHub
commit 7bdf7d09b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 2126 additions and 50 deletions

View file

@ -0,0 +1,320 @@
// run-rustfix
// The `try_into` suggestion doesn't include this, but we do suggest it after applying it
use std::convert::TryInto;
#[allow(unused_must_use)]
fn main() {
let x_usize: usize = 1;
let x_u128: u128 = 2;
let x_u64: u64 = 3;
let x_u32: u32 = 4;
let x_u16: u16 = 5;
let x_u8: u8 = 6;
let x_isize: isize = 7;
let x_i64: i64 = 8;
let x_i32: i32 = 9;
let x_i16: i16 = 10;
let x_i8: i8 = 11;
let x_i128: i128 = 12;
/* u<->u */
{
u16::from(x_u8) > x_u16;
//~^ ERROR mismatched types
u32::from(x_u8) > x_u32;
//~^ ERROR mismatched types
u64::from(x_u8) > x_u64;
//~^ ERROR mismatched types
u128::from(x_u8) > x_u128;
//~^ ERROR mismatched types
usize::from(x_u8) > x_usize;
//~^ ERROR mismatched types
x_u16 > x_u8.into();
//~^ ERROR mismatched types
u32::from(x_u16) > x_u32;
//~^ ERROR mismatched types
u64::from(x_u16) > x_u64;
//~^ ERROR mismatched types
u128::from(x_u16) > x_u128;
//~^ ERROR mismatched types
usize::from(x_u16) > x_usize;
//~^ ERROR mismatched types
x_u32 > x_u8.into();
//~^ ERROR mismatched types
x_u32 > x_u16.into();
//~^ ERROR mismatched types
u64::from(x_u32) > x_u64;
//~^ ERROR mismatched types
u128::from(x_u32) > x_u128;
//~^ ERROR mismatched types
x_u32 > x_usize.try_into().unwrap();
//~^ ERROR mismatched types
x_u64 > x_u8.into();
//~^ ERROR mismatched types
x_u64 > x_u16.into();
//~^ ERROR mismatched types
x_u64 > x_u32.into();
//~^ ERROR mismatched types
u128::from(x_u64) > x_u128;
//~^ ERROR mismatched types
x_u64 > x_usize.try_into().unwrap();
//~^ ERROR mismatched types
x_u128 > x_u8.into();
//~^ ERROR mismatched types
x_u128 > x_u16.into();
//~^ ERROR mismatched types
x_u128 > x_u32.into();
//~^ ERROR mismatched types
x_u128 > x_u64.into();
//~^ ERROR mismatched types
x_u128 > x_usize.try_into().unwrap();
//~^ ERROR mismatched types
x_usize > x_u8.into();
//~^ ERROR mismatched types
x_usize > x_u16.into();
//~^ ERROR mismatched types
x_usize > x_u32.try_into().unwrap();
//~^ ERROR mismatched types
x_usize > x_u64.try_into().unwrap();
//~^ ERROR mismatched types
x_usize > x_u128.try_into().unwrap();
//~^ ERROR mismatched types
}
/* i<->i */
{
i16::from(x_i8) > x_i16;
//~^ ERROR mismatched types
i32::from(x_i8) > x_i32;
//~^ ERROR mismatched types
i64::from(x_i8) > x_i64;
//~^ ERROR mismatched types
i128::from(x_i8) > x_i128;
//~^ ERROR mismatched types
isize::from(x_i8) > x_isize;
//~^ ERROR mismatched types
x_i16 > x_i8.into();
//~^ ERROR mismatched types
i32::from(x_i16) > x_i32;
//~^ ERROR mismatched types
i64::from(x_i16) > x_i64;
//~^ ERROR mismatched types
i128::from(x_i16) > x_i128;
//~^ ERROR mismatched types
isize::from(x_i16) > x_isize;
//~^ ERROR mismatched types
x_i32 > x_i8.into();
//~^ ERROR mismatched types
x_i32 > x_i16.into();
//~^ ERROR mismatched types
i64::from(x_i32) > x_i64;
//~^ ERROR mismatched types
i128::from(x_i32) > x_i128;
//~^ ERROR mismatched types
x_i32 > x_isize.try_into().unwrap();
//~^ ERROR mismatched types
x_i64 > x_i8.into();
//~^ ERROR mismatched types
x_i64 > x_i16.into();
//~^ ERROR mismatched types
x_i64 > x_i32.into();
//~^ ERROR mismatched types
i128::from(x_i64) > x_i128;
//~^ ERROR mismatched types
x_i64 > x_isize.try_into().unwrap();
//~^ ERROR mismatched types
x_i128 > x_i8.into();
//~^ ERROR mismatched types
x_i128 > x_i16.into();
//~^ ERROR mismatched types
x_i128 > x_i32.into();
//~^ ERROR mismatched types
x_i128 > x_i64.into();
//~^ ERROR mismatched types
x_i128 > x_isize.try_into().unwrap();
//~^ ERROR mismatched types
x_isize > x_i8.into();
//~^ ERROR mismatched types
x_isize > x_i16.into();
//~^ ERROR mismatched types
x_isize > x_i32.try_into().unwrap();
//~^ ERROR mismatched types
x_isize > x_i64.try_into().unwrap();
//~^ ERROR mismatched types
x_isize > x_i128.try_into().unwrap();
//~^ ERROR mismatched types
}
/* u<->i */
{
x_u8 > x_i8.try_into().unwrap();
//~^ ERROR mismatched types
i16::from(x_u8) > x_i16;
//~^ ERROR mismatched types
i32::from(x_u8) > x_i32;
//~^ ERROR mismatched types
i64::from(x_u8) > x_i64;
//~^ ERROR mismatched types
i128::from(x_u8) > x_i128;
//~^ ERROR mismatched types
isize::from(x_u8) > x_isize;
//~^ ERROR mismatched types
x_u16 > x_i8.try_into().unwrap();
//~^ ERROR mismatched types
x_u16 > x_i16.try_into().unwrap();
//~^ ERROR mismatched types
i32::from(x_u16) > x_i32;
//~^ ERROR mismatched types
i64::from(x_u16) > x_i64;
//~^ ERROR mismatched types
i128::from(x_u16) > x_i128;
//~^ ERROR mismatched types
x_u16 > x_isize.try_into().unwrap();
//~^ ERROR mismatched types
x_u32 > x_i8.try_into().unwrap();
//~^ ERROR mismatched types
x_u32 > x_i16.try_into().unwrap();
//~^ ERROR mismatched types
x_u32 > x_i32.try_into().unwrap();
//~^ ERROR mismatched types
i64::from(x_u32) > x_i64;
//~^ ERROR mismatched types
i128::from(x_u32) > x_i128;
//~^ ERROR mismatched types
x_u32 > x_isize.try_into().unwrap();
//~^ ERROR mismatched types
x_u64 > x_i8.try_into().unwrap();
//~^ ERROR mismatched types
x_u64 > x_i16.try_into().unwrap();
//~^ ERROR mismatched types
x_u64 > x_i32.try_into().unwrap();
//~^ ERROR mismatched types
x_u64 > x_i64.try_into().unwrap();
//~^ ERROR mismatched types
i128::from(x_u64) > x_i128;
//~^ ERROR mismatched types
x_u64 > x_isize.try_into().unwrap();
//~^ ERROR mismatched types
x_u128 > x_i8.try_into().unwrap();
//~^ ERROR mismatched types
x_u128 > x_i16.try_into().unwrap();
//~^ ERROR mismatched types
x_u128 > x_i32.try_into().unwrap();
//~^ ERROR mismatched types
x_u128 > x_i64.try_into().unwrap();
//~^ ERROR mismatched types
x_u128 > x_i128.try_into().unwrap();
//~^ ERROR mismatched types
x_u128 > x_isize.try_into().unwrap();
//~^ ERROR mismatched types
x_usize > x_i8.try_into().unwrap();
//~^ ERROR mismatched types
x_usize > x_i16.try_into().unwrap();
//~^ ERROR mismatched types
x_usize > x_i32.try_into().unwrap();
//~^ ERROR mismatched types
x_usize > x_i64.try_into().unwrap();
//~^ ERROR mismatched types
x_usize > x_i128.try_into().unwrap();
//~^ ERROR mismatched types
x_usize > x_isize.try_into().unwrap();
//~^ ERROR mismatched types
}
/* i<->u */
{
x_i8 > x_u8.try_into().unwrap();
//~^ ERROR mismatched types
x_i8 > x_u16.try_into().unwrap();
//~^ ERROR mismatched types
x_i8 > x_u32.try_into().unwrap();
//~^ ERROR mismatched types
x_i8 > x_u64.try_into().unwrap();
//~^ ERROR mismatched types
x_i8 > x_u128.try_into().unwrap();
//~^ ERROR mismatched types
x_i8 > x_usize.try_into().unwrap();
//~^ ERROR mismatched types
x_i16 > x_u8.into();
//~^ ERROR mismatched types
x_i16 > x_u16.try_into().unwrap();
//~^ ERROR mismatched types
x_i16 > x_u32.try_into().unwrap();
//~^ ERROR mismatched types
x_i16 > x_u64.try_into().unwrap();
//~^ ERROR mismatched types
x_i16 > x_u128.try_into().unwrap();
//~^ ERROR mismatched types
x_i16 > x_usize.try_into().unwrap();
//~^ ERROR mismatched types
x_i32 > x_u8.into();
//~^ ERROR mismatched types
x_i32 > x_u16.into();
//~^ ERROR mismatched types
x_i32 > x_u32.try_into().unwrap();
//~^ ERROR mismatched types
x_i32 > x_u64.try_into().unwrap();
//~^ ERROR mismatched types
x_i32 > x_u128.try_into().unwrap();
//~^ ERROR mismatched types
x_i32 > x_usize.try_into().unwrap();
//~^ ERROR mismatched types
x_i64 > x_u8.into();
//~^ ERROR mismatched types
x_i64 > x_u16.into();
//~^ ERROR mismatched types
x_i64 > x_u32.into();
//~^ ERROR mismatched types
x_i64 > x_u64.try_into().unwrap();
//~^ ERROR mismatched types
x_i64 > x_u128.try_into().unwrap();
//~^ ERROR mismatched types
x_i64 > x_usize.try_into().unwrap();
//~^ ERROR mismatched types
x_i128 > x_u8.into();
//~^ ERROR mismatched types
x_i128 > x_u16.into();
//~^ ERROR mismatched types
x_i128 > x_u32.into();
//~^ ERROR mismatched types
x_i128 > x_u64.into();
//~^ ERROR mismatched types
x_i128 > x_u128.try_into().unwrap();
//~^ ERROR mismatched types
x_i128 > x_usize.try_into().unwrap();
//~^ ERROR mismatched types
x_isize > x_u8.into();
//~^ ERROR mismatched types
x_isize > x_u16.try_into().unwrap();
//~^ ERROR mismatched types
x_isize > x_u32.try_into().unwrap();
//~^ ERROR mismatched types
x_isize > x_u64.try_into().unwrap();
//~^ ERROR mismatched types
x_isize > x_u128.try_into().unwrap();
//~^ ERROR mismatched types
x_isize > x_usize.try_into().unwrap();
//~^ ERROR mismatched types
}
}

View file

@ -0,0 +1,320 @@
// run-rustfix
// The `try_into` suggestion doesn't include this, but we do suggest it after applying it
use std::convert::TryInto;
#[allow(unused_must_use)]
fn main() {
let x_usize: usize = 1;
let x_u128: u128 = 2;
let x_u64: u64 = 3;
let x_u32: u32 = 4;
let x_u16: u16 = 5;
let x_u8: u8 = 6;
let x_isize: isize = 7;
let x_i64: i64 = 8;
let x_i32: i32 = 9;
let x_i16: i16 = 10;
let x_i8: i8 = 11;
let x_i128: i128 = 12;
/* u<->u */
{
x_u8 > x_u16;
//~^ ERROR mismatched types
x_u8 > x_u32;
//~^ ERROR mismatched types
x_u8 > x_u64;
//~^ ERROR mismatched types
x_u8 > x_u128;
//~^ ERROR mismatched types
x_u8 > x_usize;
//~^ ERROR mismatched types
x_u16 > x_u8;
//~^ ERROR mismatched types
x_u16 > x_u32;
//~^ ERROR mismatched types
x_u16 > x_u64;
//~^ ERROR mismatched types
x_u16 > x_u128;
//~^ ERROR mismatched types
x_u16 > x_usize;
//~^ ERROR mismatched types
x_u32 > x_u8;
//~^ ERROR mismatched types
x_u32 > x_u16;
//~^ ERROR mismatched types
x_u32 > x_u64;
//~^ ERROR mismatched types
x_u32 > x_u128;
//~^ ERROR mismatched types
x_u32 > x_usize;
//~^ ERROR mismatched types
x_u64 > x_u8;
//~^ ERROR mismatched types
x_u64 > x_u16;
//~^ ERROR mismatched types
x_u64 > x_u32;
//~^ ERROR mismatched types
x_u64 > x_u128;
//~^ ERROR mismatched types
x_u64 > x_usize;
//~^ ERROR mismatched types
x_u128 > x_u8;
//~^ ERROR mismatched types
x_u128 > x_u16;
//~^ ERROR mismatched types
x_u128 > x_u32;
//~^ ERROR mismatched types
x_u128 > x_u64;
//~^ ERROR mismatched types
x_u128 > x_usize;
//~^ ERROR mismatched types
x_usize > x_u8;
//~^ ERROR mismatched types
x_usize > x_u16;
//~^ ERROR mismatched types
x_usize > x_u32;
//~^ ERROR mismatched types
x_usize > x_u64;
//~^ ERROR mismatched types
x_usize > x_u128;
//~^ ERROR mismatched types
}
/* i<->i */
{
x_i8 > x_i16;
//~^ ERROR mismatched types
x_i8 > x_i32;
//~^ ERROR mismatched types
x_i8 > x_i64;
//~^ ERROR mismatched types
x_i8 > x_i128;
//~^ ERROR mismatched types
x_i8 > x_isize;
//~^ ERROR mismatched types
x_i16 > x_i8;
//~^ ERROR mismatched types
x_i16 > x_i32;
//~^ ERROR mismatched types
x_i16 > x_i64;
//~^ ERROR mismatched types
x_i16 > x_i128;
//~^ ERROR mismatched types
x_i16 > x_isize;
//~^ ERROR mismatched types
x_i32 > x_i8;
//~^ ERROR mismatched types
x_i32 > x_i16;
//~^ ERROR mismatched types
x_i32 > x_i64;
//~^ ERROR mismatched types
x_i32 > x_i128;
//~^ ERROR mismatched types
x_i32 > x_isize;
//~^ ERROR mismatched types
x_i64 > x_i8;
//~^ ERROR mismatched types
x_i64 > x_i16;
//~^ ERROR mismatched types
x_i64 > x_i32;
//~^ ERROR mismatched types
x_i64 > x_i128;
//~^ ERROR mismatched types
x_i64 > x_isize;
//~^ ERROR mismatched types
x_i128 > x_i8;
//~^ ERROR mismatched types
x_i128 > x_i16;
//~^ ERROR mismatched types
x_i128 > x_i32;
//~^ ERROR mismatched types
x_i128 > x_i64;
//~^ ERROR mismatched types
x_i128 > x_isize;
//~^ ERROR mismatched types
x_isize > x_i8;
//~^ ERROR mismatched types
x_isize > x_i16;
//~^ ERROR mismatched types
x_isize > x_i32;
//~^ ERROR mismatched types
x_isize > x_i64;
//~^ ERROR mismatched types
x_isize > x_i128;
//~^ ERROR mismatched types
}
/* u<->i */
{
x_u8 > x_i8;
//~^ ERROR mismatched types
x_u8 > x_i16;
//~^ ERROR mismatched types
x_u8 > x_i32;
//~^ ERROR mismatched types
x_u8 > x_i64;
//~^ ERROR mismatched types
x_u8 > x_i128;
//~^ ERROR mismatched types
x_u8 > x_isize;
//~^ ERROR mismatched types
x_u16 > x_i8;
//~^ ERROR mismatched types
x_u16 > x_i16;
//~^ ERROR mismatched types
x_u16 > x_i32;
//~^ ERROR mismatched types
x_u16 > x_i64;
//~^ ERROR mismatched types
x_u16 > x_i128;
//~^ ERROR mismatched types
x_u16 > x_isize;
//~^ ERROR mismatched types
x_u32 > x_i8;
//~^ ERROR mismatched types
x_u32 > x_i16;
//~^ ERROR mismatched types
x_u32 > x_i32;
//~^ ERROR mismatched types
x_u32 > x_i64;
//~^ ERROR mismatched types
x_u32 > x_i128;
//~^ ERROR mismatched types
x_u32 > x_isize;
//~^ ERROR mismatched types
x_u64 > x_i8;
//~^ ERROR mismatched types
x_u64 > x_i16;
//~^ ERROR mismatched types
x_u64 > x_i32;
//~^ ERROR mismatched types
x_u64 > x_i64;
//~^ ERROR mismatched types
x_u64 > x_i128;
//~^ ERROR mismatched types
x_u64 > x_isize;
//~^ ERROR mismatched types
x_u128 > x_i8;
//~^ ERROR mismatched types
x_u128 > x_i16;
//~^ ERROR mismatched types
x_u128 > x_i32;
//~^ ERROR mismatched types
x_u128 > x_i64;
//~^ ERROR mismatched types
x_u128 > x_i128;
//~^ ERROR mismatched types
x_u128 > x_isize;
//~^ ERROR mismatched types
x_usize > x_i8;
//~^ ERROR mismatched types
x_usize > x_i16;
//~^ ERROR mismatched types
x_usize > x_i32;
//~^ ERROR mismatched types
x_usize > x_i64;
//~^ ERROR mismatched types
x_usize > x_i128;
//~^ ERROR mismatched types
x_usize > x_isize;
//~^ ERROR mismatched types
}
/* i<->u */
{
x_i8 > x_u8;
//~^ ERROR mismatched types
x_i8 > x_u16;
//~^ ERROR mismatched types
x_i8 > x_u32;
//~^ ERROR mismatched types
x_i8 > x_u64;
//~^ ERROR mismatched types
x_i8 > x_u128;
//~^ ERROR mismatched types
x_i8 > x_usize;
//~^ ERROR mismatched types
x_i16 > x_u8;
//~^ ERROR mismatched types
x_i16 > x_u16;
//~^ ERROR mismatched types
x_i16 > x_u32;
//~^ ERROR mismatched types
x_i16 > x_u64;
//~^ ERROR mismatched types
x_i16 > x_u128;
//~^ ERROR mismatched types
x_i16 > x_usize;
//~^ ERROR mismatched types
x_i32 > x_u8;
//~^ ERROR mismatched types
x_i32 > x_u16;
//~^ ERROR mismatched types
x_i32 > x_u32;
//~^ ERROR mismatched types
x_i32 > x_u64;
//~^ ERROR mismatched types
x_i32 > x_u128;
//~^ ERROR mismatched types
x_i32 > x_usize;
//~^ ERROR mismatched types
x_i64 > x_u8;
//~^ ERROR mismatched types
x_i64 > x_u16;
//~^ ERROR mismatched types
x_i64 > x_u32;
//~^ ERROR mismatched types
x_i64 > x_u64;
//~^ ERROR mismatched types
x_i64 > x_u128;
//~^ ERROR mismatched types
x_i64 > x_usize;
//~^ ERROR mismatched types
x_i128 > x_u8;
//~^ ERROR mismatched types
x_i128 > x_u16;
//~^ ERROR mismatched types
x_i128 > x_u32;
//~^ ERROR mismatched types
x_i128 > x_u64;
//~^ ERROR mismatched types
x_i128 > x_u128;
//~^ ERROR mismatched types
x_i128 > x_usize;
//~^ ERROR mismatched types
x_isize > x_u8;
//~^ ERROR mismatched types
x_isize > x_u16;
//~^ ERROR mismatched types
x_isize > x_u32;
//~^ ERROR mismatched types
x_isize > x_u64;
//~^ ERROR mismatched types
x_isize > x_u128;
//~^ ERROR mismatched types
x_isize > x_usize;
//~^ ERROR mismatched types
}
}

File diff suppressed because it is too large Load diff