Only suggest casting numeric types using into()
This commit is contained in:
parent
af91d9955b
commit
509ea8efc6
6 changed files with 494 additions and 989 deletions
|
|
@ -331,13 +331,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
|||
// We want to minimize the amount of casting operations that are suggested, as it can be a
|
||||
// lossy operation with potentially bad side effects, so we only suggest when encountering
|
||||
// an expression that indicates that the original type couldn't be directly changed.
|
||||
let can_cast = match expr.node {
|
||||
hir::ExprPath(..) |
|
||||
hir::ExprCall(..) |
|
||||
hir::ExprMethodCall(..) |
|
||||
hir::ExprBinary(..) => true,
|
||||
_ => false,
|
||||
};
|
||||
//
|
||||
// For now, don't suggest casting with `as`.
|
||||
let can_cast = false;
|
||||
|
||||
let needs_paren = match expr.node {
|
||||
hir::ExprBinary(..) => true,
|
||||
|
|
@ -369,7 +365,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
|||
(None, _) | (_, None) => {
|
||||
if can_cast {
|
||||
err.span_suggestion(expr.span,
|
||||
&format!("{}, which {}", msg, depending_on_isize),
|
||||
&format!("{}, which {}",
|
||||
msg,
|
||||
depending_on_isize),
|
||||
cast_suggestion);
|
||||
}
|
||||
}
|
||||
|
|
@ -393,7 +391,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
|||
(None, _) | (_, None) => {
|
||||
if can_cast {
|
||||
err.span_suggestion(expr.span,
|
||||
&format!("{}, which {}", msg, depending_on_usize),
|
||||
&format!("{}, which {}",
|
||||
msg,
|
||||
depending_on_usize),
|
||||
cast_suggestion);
|
||||
}
|
||||
}
|
||||
|
|
@ -420,12 +420,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
|||
}
|
||||
(None, _) => {
|
||||
err.span_suggestion(expr.span,
|
||||
&format!("{}, which {}", msg, depending_on_isize),
|
||||
&format!("{}, which {}",
|
||||
msg,
|
||||
depending_on_isize),
|
||||
cast_suggestion);
|
||||
}
|
||||
(_, None) => {
|
||||
err.span_suggestion(expr.span,
|
||||
&format!("{}, which {}", msg, depending_on_usize),
|
||||
&format!("{}, which {}",
|
||||
msg,
|
||||
depending_on_usize),
|
||||
cast_suggestion);
|
||||
}
|
||||
_ => {
|
||||
|
|
@ -452,12 +456,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
|||
}
|
||||
(None, _) => {
|
||||
err.span_suggestion(expr.span,
|
||||
&format!("{}, which {}", msg, depending_on_usize),
|
||||
&format!("{}, which {}",
|
||||
msg,
|
||||
depending_on_usize),
|
||||
cast_suggestion);
|
||||
}
|
||||
(_, None) => {
|
||||
err.span_suggestion(expr.span,
|
||||
&format!("{}, which {}", msg, depending_on_isize),
|
||||
&format!("{}, which {}",
|
||||
msg,
|
||||
depending_on_isize),
|
||||
cast_suggestion);
|
||||
}
|
||||
_ => {
|
||||
|
|
|
|||
|
|
@ -6,10 +6,6 @@ error[E0308]: mismatched types
|
|||
...
|
||||
37 | write!(hello);
|
||||
| -------------- in this macro invocation
|
||||
help: you can cast an `usize` to `u64`, which will truncate or zero-extend depending on the bit width of `usize`
|
||||
|
|
||||
26 | ($arr.len() * size_of($arr[0])) as u64); //~ ERROR mismatched types
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0605]: non-primitive cast: `{integer}` as `()`
|
||||
--> $DIR/issue-26480.rs:32:19
|
||||
|
|
|
|||
|
|
@ -12,7 +12,9 @@ fn foo() -> i32 {
|
|||
4
|
||||
}
|
||||
fn main() {
|
||||
let x: u32 = foo();
|
||||
let x: u16 = foo();
|
||||
//~^ ERROR mismatched types
|
||||
let y: i64 = x + x;
|
||||
//~^ ERROR mismatched types
|
||||
let z: i32 = x + x;
|
||||
//~^ ERROR mismatched types
|
||||
|
|
|
|||
|
|
@ -1,22 +1,20 @@
|
|||
error[E0308]: mismatched types
|
||||
--> $DIR/numeric-cast-2.rs:15:18
|
||||
|
|
||||
15 | let x: u32 = foo();
|
||||
| ^^^^^ expected u32, found i32
|
||||
help: you can cast an `i32` to `u32`, which will sign-extend the source value
|
||||
|
|
||||
15 | let x: u32 = foo() as u32;
|
||||
| ^^^^^^^^^^^^
|
||||
15 | let x: u16 = foo();
|
||||
| ^^^^^ expected u16, found i32
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/numeric-cast-2.rs:17:18
|
||||
|
|
||||
17 | let z: i32 = x + x;
|
||||
| ^^^^^ expected i32, found u32
|
||||
help: you can cast an `u32` to `i32`, which will truncate the source value
|
||||
17 | let y: i64 = x + x;
|
||||
| ^^^^^ expected i64, found u16
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/numeric-cast-2.rs:19:18
|
||||
|
|
||||
17 | let z: i32 = (x + x) as i32;
|
||||
| ^^^^^^^^^^^^^^
|
||||
19 | let z: i32 = x + x;
|
||||
| ^^^^^ expected i32, found u16
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
|
|
|
|||
|
|
@ -46,10 +46,8 @@ fn main() {
|
|||
//~^ ERROR mismatched types
|
||||
foo::<usize>(x_f64);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
foo::<usize>(x_f32);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
|
||||
foo::<isize>(x_usize);
|
||||
//~^ ERROR mismatched types
|
||||
|
|
@ -72,10 +70,8 @@ fn main() {
|
|||
//~^ ERROR mismatched types
|
||||
foo::<isize>(x_f64);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
foo::<isize>(x_f32);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
|
||||
foo::<u64>(x_usize);
|
||||
//~^ ERROR mismatched types
|
||||
|
|
@ -98,10 +94,8 @@ fn main() {
|
|||
//~^ ERROR mismatched types
|
||||
foo::<u64>(x_f64);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
foo::<u64>(x_f32);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
|
||||
foo::<i64>(x_usize);
|
||||
//~^ ERROR mismatched types
|
||||
|
|
@ -124,10 +118,8 @@ fn main() {
|
|||
//~^ ERROR mismatched types
|
||||
foo::<i64>(x_f64);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
foo::<i64>(x_f32);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
|
||||
foo::<u32>(x_usize);
|
||||
//~^ ERROR mismatched types
|
||||
|
|
@ -150,10 +142,8 @@ fn main() {
|
|||
//~^ ERROR mismatched types
|
||||
foo::<u32>(x_f64);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
foo::<u32>(x_f32);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
|
||||
foo::<i32>(x_usize);
|
||||
//~^ ERROR mismatched types
|
||||
|
|
@ -176,10 +166,8 @@ fn main() {
|
|||
//~^ ERROR mismatched types
|
||||
foo::<i32>(x_f64);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
foo::<i32>(x_f32);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
|
||||
foo::<u16>(x_usize);
|
||||
//~^ ERROR mismatched types
|
||||
|
|
@ -202,10 +190,8 @@ fn main() {
|
|||
//~^ ERROR mismatched types
|
||||
foo::<u16>(x_f64);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
foo::<u16>(x_f32);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
|
||||
foo::<i16>(x_usize);
|
||||
//~^ ERROR mismatched types
|
||||
|
|
@ -228,10 +214,8 @@ fn main() {
|
|||
//~^ ERROR mismatched types
|
||||
foo::<i16>(x_f64);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
foo::<i16>(x_f32);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
|
||||
foo::<u8>(x_usize);
|
||||
//~^ ERROR mismatched types
|
||||
|
|
@ -254,10 +238,8 @@ fn main() {
|
|||
//~^ ERROR mismatched types
|
||||
foo::<u8>(x_f64);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
foo::<u8>(x_f32);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
|
||||
foo::<i8>(x_usize);
|
||||
//~^ ERROR mismatched types
|
||||
|
|
@ -280,10 +262,8 @@ fn main() {
|
|||
foo::<i8>(x_i8);
|
||||
foo::<i8>(x_f64);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
foo::<i8>(x_f32);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause Undefined Behavior
|
||||
|
||||
foo::<f64>(x_usize);
|
||||
//~^ ERROR mismatched types
|
||||
|
|
@ -331,6 +311,5 @@ fn main() {
|
|||
//~^ ERROR mismatched types
|
||||
foo::<f32>(x_f64);
|
||||
//~^ ERROR mismatched types
|
||||
//~| WARN casting here will cause undefined behavior
|
||||
foo::<f32>(x_f32);
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue