mirror of
https://github.com/rust-lang/rust
synced 2024-10-02 06:44:33 +00:00
provide suggestion for invalid boolean cast
Also, don't suggest comparing to zero for non-numeric expressions.
This commit is contained in:
parent
6ecad33838
commit
565c39de43
|
@ -251,10 +251,28 @@ fn report_cast_error(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, e: CastError) {
|
|||
.emit();
|
||||
}
|
||||
CastError::CastToBool => {
|
||||
struct_span_err!(fcx.tcx.sess, self.span, E0054, "cannot cast as `bool`")
|
||||
.span_label(self.span, "unsupported cast")
|
||||
.help("compare with zero instead")
|
||||
.emit();
|
||||
let mut err =
|
||||
struct_span_err!(fcx.tcx.sess, self.span, E0054, "cannot cast as `bool`");
|
||||
|
||||
if self.expr_ty.is_numeric() {
|
||||
match fcx.tcx.sess.source_map().span_to_snippet(self.expr.span) {
|
||||
Ok(snippet) => {
|
||||
err.span_suggestion_with_applicability(
|
||||
self.span,
|
||||
"compare with zero instead",
|
||||
format!("{} != 0", snippet),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
Err(_) => {
|
||||
err.span_help(self.span, "compare with zero instead");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err.span_label(self.span, "unsupported cast");
|
||||
}
|
||||
|
||||
err.emit();
|
||||
}
|
||||
CastError::CastToChar => {
|
||||
type_error_struct!(fcx.tcx.sess, self.span, self.expr_ty, E0604,
|
||||
|
|
|
@ -1,4 +1,9 @@
|
|||
fn main() {
|
||||
let u = 5 as bool;
|
||||
//~^ ERROR cannot cast as `bool`
|
||||
let u = 5 as bool; //~ ERROR cannot cast as `bool`
|
||||
//~| HELP compare with zero instead
|
||||
//~| SUGGESTION 5 != 0
|
||||
let t = (1 + 2) as bool; //~ ERROR cannot cast as `bool`
|
||||
//~| HELP compare with zero instead
|
||||
//~| SUGGESTION (1 + 2) != 0
|
||||
let v = "hello" as bool; //~ ERROR cannot cast as `bool`
|
||||
}
|
||||
|
|
|
@ -1,11 +1,21 @@
|
|||
error[E0054]: cannot cast as `bool`
|
||||
--> $DIR/cast-as-bool.rs:2:13
|
||||
|
|
||||
LL | let u = 5 as bool;
|
||||
| ^^^^^^^^^ unsupported cast
|
||||
|
|
||||
= help: compare with zero instead
|
||||
LL | let u = 5 as bool; //~ ERROR cannot cast as `bool`
|
||||
| ^^^^^^^^^ help: compare with zero instead: `5 != 0`
|
||||
|
||||
error: aborting due to previous error
|
||||
error[E0054]: cannot cast as `bool`
|
||||
--> $DIR/cast-as-bool.rs:5:13
|
||||
|
|
||||
LL | let t = (1 + 2) as bool; //~ ERROR cannot cast as `bool`
|
||||
| ^^^^^^^^^^^^^^^ help: compare with zero instead: `(1 + 2) != 0`
|
||||
|
||||
error[E0054]: cannot cast as `bool`
|
||||
--> $DIR/cast-as-bool.rs:8:13
|
||||
|
|
||||
LL | let v = "hello" as bool; //~ ERROR cannot cast as `bool`
|
||||
| ^^^^^^^^^^^^^^^ unsupported cast
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0054`.
|
||||
|
|
|
@ -2,9 +2,7 @@ error[E0054]: cannot cast as `bool`
|
|||
--> $DIR/cast-rfc0401-2.rs:6:13
|
||||
|
|
||||
LL | let _ = 3 as bool;
|
||||
| ^^^^^^^^^ unsupported cast
|
||||
|
|
||||
= help: compare with zero instead
|
||||
| ^^^^^^^^^ help: compare with zero instead: `3 != 0`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
@ -2,9 +2,7 @@ error[E0054]: cannot cast as `bool`
|
|||
--> $DIR/E0054.rs:3:24
|
||||
|
|
||||
LL | let x_is_nonzero = x as bool; //~ ERROR E0054
|
||||
| ^^^^^^^^^ unsupported cast
|
||||
|
|
||||
= help: compare with zero instead
|
||||
| ^^^^^^^^^ help: compare with zero instead: `x != 0`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
@ -52,9 +52,7 @@ error[E0054]: cannot cast as `bool`
|
|||
--> $DIR/error-festival.rs:33:24
|
||||
|
|
||||
LL | let x_is_nonzero = x as bool;
|
||||
| ^^^^^^^^^ unsupported cast
|
||||
|
|
||||
= help: compare with zero instead
|
||||
| ^^^^^^^^^ help: compare with zero instead: `x != 0`
|
||||
|
||||
error[E0606]: casting `&u8` as `u32` is invalid
|
||||
--> $DIR/error-festival.rs:37:18
|
||||
|
|
|
@ -90,17 +90,13 @@ error[E0054]: cannot cast as `bool`
|
|||
--> $DIR/cast-rfc0401.rs:39:13
|
||||
|
|
||||
LL | let _ = 3_i32 as bool; //~ ERROR cannot cast
|
||||
| ^^^^^^^^^^^^^ unsupported cast
|
||||
|
|
||||
= help: compare with zero instead
|
||||
| ^^^^^^^^^^^^^ help: compare with zero instead: `3_i32 != 0`
|
||||
|
||||
error[E0054]: cannot cast as `bool`
|
||||
--> $DIR/cast-rfc0401.rs:40:13
|
||||
|
|
||||
LL | let _ = E::A as bool; //~ ERROR cannot cast
|
||||
| ^^^^^^^^^^^^ unsupported cast
|
||||
|
|
||||
= help: compare with zero instead
|
||||
|
||||
error[E0604]: only `u8` can be cast as `char`, not `u32`
|
||||
--> $DIR/cast-rfc0401.rs:41:13
|
||||
|
|
Loading…
Reference in a new issue