Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide suggestion for invalid boolean cast #57481

Merged
merged 1 commit into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/librustc_typeck/check/cast.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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