Skip to content

Commit

Permalink
provide suggestion for invalid boolean cast
Browse files Browse the repository at this point in the history
Also, don't suggest comparing to zero for non-numeric expressions.
  • Loading branch information
euclio committed Jan 9, 2019
1 parent 6ecad33 commit 565c39d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 24 deletions.
26 changes: 22 additions & 4 deletions src/librustc_typeck/check/cast.rs
Expand Up @@ -251,10 +251,28 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
.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,
Expand Down
9 changes: 7 additions & 2 deletions src/test/ui/cast/cast-as-bool.rs
@@ -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`
}
18 changes: 14 additions & 4 deletions src/test/ui/cast/cast-as-bool.stderr
@@ -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
LL | let u = 5 as bool; //~ ERROR cannot cast as `bool`
| ^^^^^^^^^ help: compare with zero instead: `5 != 0`

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
|
= help: compare with zero instead
LL | let v = "hello" as bool; //~ ERROR cannot cast as `bool`
| ^^^^^^^^^^^^^^^ unsupported cast

error: aborting due to previous error
error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0054`.
4 changes: 1 addition & 3 deletions src/test/ui/cast/cast-rfc0401-2.stderr
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions src/test/ui/error-codes/E0054.stderr
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions src/test/ui/error-festival.stderr
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/test/ui/mismatched_types/cast-rfc0401.stderr
Expand Up @@ -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
Expand Down

0 comments on commit 565c39d

Please sign in to comment.