Use s::u::p::expr_precedence and fix message

- Use `syntax::util::parser::expr_precedence` to determine wether
   parenthesis are needed around the casting target.
 - Update message to not incorrectly mention rounding on `.into()`
   suggestions, as those types that do have that implemented will never
   round.
This commit is contained in:
Esteban Küber 2018-01-09 18:41:24 -08:00
parent 509ea8efc6
commit 09efaaf076
2 changed files with 16 additions and 18 deletions

View File

@ -15,6 +15,7 @@ use rustc::infer::InferOk;
use rustc::traits::ObligationCause;
use syntax::ast;
use syntax::util::parser::{expr_precedence, AssocOp};
use syntax_pos::{self, Span};
use rustc::hir;
use rustc::hir::print;
@ -335,10 +336,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// For now, don't suggest casting with `as`.
let can_cast = false;
let needs_paren = match expr.node {
hir::ExprBinary(..) => true,
_ => false,
};
let needs_paren = expr_precedence(expr) < (AssocOp::As.precedence() as i8);
if let Ok(src) = self.tcx.sess.codemap().span_to_snippet(expr.span) {
let msg = format!("you can cast an `{}` to `{}`", checked_ty, expected_ty);
@ -508,11 +506,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
true
}
(&ty::TyFloat(ref exp), &ty::TyUint(ref found)) => {
// if `found` is `None` (meaning found is `usize`), don't suggest `.into()`
if exp.bit_width() > found.bit_width().unwrap_or(256) {
err.span_suggestion(expr.span,
&format!("{}, producing the floating point \
representation of the integer, rounded if \
necessary",
representation of the integer",
msg),
into_suggestion);
} else if can_cast {
@ -526,11 +524,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
true
}
(&ty::TyFloat(ref exp), &ty::TyInt(ref found)) => {
// if `found` is `None` (meaning found is `isize`), don't suggest `.into()`
if exp.bit_width() > found.bit_width().unwrap_or(256) {
err.span_suggestion(expr.span,
&format!("{}, producing the floating point \
representation of the integer, rounded if \
necessary",
representation of the integer",
msg),
into_suggestion);
} else if can_cast {

View File

@ -723,7 +723,7 @@ error[E0308]: mismatched types
|
272 | foo::<f64>(x_u32);
| ^^^^^ expected f64, found u32
help: you can cast an `u32` to `f64`, producing the floating point representation of the integer, rounded if necessary
help: you can cast an `u32` to `f64`, producing the floating point representation of the integer
|
272 | foo::<f64>(x_u32.into());
| ^^^^^^^^^^^^
@ -733,7 +733,7 @@ error[E0308]: mismatched types
|
274 | foo::<f64>(x_u16);
| ^^^^^ expected f64, found u16
help: you can cast an `u16` to `f64`, producing the floating point representation of the integer, rounded if necessary
help: you can cast an `u16` to `f64`, producing the floating point representation of the integer
|
274 | foo::<f64>(x_u16.into());
| ^^^^^^^^^^^^
@ -743,7 +743,7 @@ error[E0308]: mismatched types
|
276 | foo::<f64>(x_u8);
| ^^^^ expected f64, found u8
help: you can cast an `u8` to `f64`, producing the floating point representation of the integer, rounded if necessary
help: you can cast an `u8` to `f64`, producing the floating point representation of the integer
|
276 | foo::<f64>(x_u8.into());
| ^^^^^^^^^^^
@ -765,7 +765,7 @@ error[E0308]: mismatched types
|
282 | foo::<f64>(x_i32);
| ^^^^^ expected f64, found i32
help: you can cast an `i32` to `f64`, producing the floating point representation of the integer, rounded if necessary
help: you can cast an `i32` to `f64`, producing the floating point representation of the integer
|
282 | foo::<f64>(x_i32.into());
| ^^^^^^^^^^^^
@ -775,7 +775,7 @@ error[E0308]: mismatched types
|
284 | foo::<f64>(x_i16);
| ^^^^^ expected f64, found i16
help: you can cast an `i16` to `f64`, producing the floating point representation of the integer, rounded if necessary
help: you can cast an `i16` to `f64`, producing the floating point representation of the integer
|
284 | foo::<f64>(x_i16.into());
| ^^^^^^^^^^^^
@ -785,7 +785,7 @@ error[E0308]: mismatched types
|
286 | foo::<f64>(x_i8);
| ^^^^ expected f64, found i8
help: you can cast an `i8` to `f64`, producing the floating point representation of the integer, rounded if necessary
help: you can cast an `i8` to `f64`, producing the floating point representation of the integer
|
286 | foo::<f64>(x_i8.into());
| ^^^^^^^^^^^
@ -823,7 +823,7 @@ error[E0308]: mismatched types
|
298 | foo::<f32>(x_u16);
| ^^^^^ expected f32, found u16
help: you can cast an `u16` to `f32`, producing the floating point representation of the integer, rounded if necessary
help: you can cast an `u16` to `f32`, producing the floating point representation of the integer
|
298 | foo::<f32>(x_u16.into());
| ^^^^^^^^^^^^
@ -833,7 +833,7 @@ error[E0308]: mismatched types
|
300 | foo::<f32>(x_u8);
| ^^^^ expected f32, found u8
help: you can cast an `u8` to `f32`, producing the floating point representation of the integer, rounded if necessary
help: you can cast an `u8` to `f32`, producing the floating point representation of the integer
|
300 | foo::<f32>(x_u8.into());
| ^^^^^^^^^^^
@ -861,7 +861,7 @@ error[E0308]: mismatched types
|
308 | foo::<f32>(x_i16);
| ^^^^^ expected f32, found i16
help: you can cast an `i16` to `f32`, producing the floating point representation of the integer, rounded if necessary
help: you can cast an `i16` to `f32`, producing the floating point representation of the integer
|
308 | foo::<f32>(x_i16.into());
| ^^^^^^^^^^^^
@ -871,7 +871,7 @@ error[E0308]: mismatched types
|
310 | foo::<f32>(x_i8);
| ^^^^ expected f32, found i8
help: you can cast an `i8` to `f32`, producing the floating point representation of the integer, rounded if necessary
help: you can cast an `i8` to `f32`, producing the floating point representation of the integer
|
310 | foo::<f32>(x_i8.into());
| ^^^^^^^^^^^