Skip to content

Commit e915165

Browse files
authored
Rollup merge of rust-lang#57481 - euclio:bool-cast-suggestion, r=estebank
provide suggestion for invalid boolean cast Also, don't suggest comparing to zero for non-numeric expressions.
2 parents 55d835c + 565c39d commit e915165

File tree

7 files changed

+47
-24
lines changed

7 files changed

+47
-24
lines changed

src/librustc_typeck/check/cast.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,28 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
257257
.emit();
258258
}
259259
CastError::CastToBool => {
260-
struct_span_err!(fcx.tcx.sess, self.span, E0054, "cannot cast as `bool`")
261-
.span_label(self.span, "unsupported cast")
262-
.help("compare with zero instead")
263-
.emit();
260+
let mut err =
261+
struct_span_err!(fcx.tcx.sess, self.span, E0054, "cannot cast as `bool`");
262+
263+
if self.expr_ty.is_numeric() {
264+
match fcx.tcx.sess.source_map().span_to_snippet(self.expr.span) {
265+
Ok(snippet) => {
266+
err.span_suggestion_with_applicability(
267+
self.span,
268+
"compare with zero instead",
269+
format!("{} != 0", snippet),
270+
Applicability::MachineApplicable,
271+
);
272+
}
273+
Err(_) => {
274+
err.span_help(self.span, "compare with zero instead");
275+
}
276+
}
277+
} else {
278+
err.span_label(self.span, "unsupported cast");
279+
}
280+
281+
err.emit();
264282
}
265283
CastError::CastToChar => {
266284
type_error_struct!(fcx.tcx.sess, self.span, self.expr_ty, E0604,

src/test/ui/cast/cast-as-bool.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
fn main() {
2-
let u = 5 as bool;
3-
//~^ ERROR cannot cast as `bool`
2+
let u = 5 as bool; //~ ERROR cannot cast as `bool`
3+
//~| HELP compare with zero instead
4+
//~| SUGGESTION 5 != 0
5+
let t = (1 + 2) as bool; //~ ERROR cannot cast as `bool`
6+
//~| HELP compare with zero instead
7+
//~| SUGGESTION (1 + 2) != 0
8+
let v = "hello" as bool; //~ ERROR cannot cast as `bool`
49
}

src/test/ui/cast/cast-as-bool.stderr

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
error[E0054]: cannot cast as `bool`
22
--> $DIR/cast-as-bool.rs:2:13
33
|
4-
LL | let u = 5 as bool;
5-
| ^^^^^^^^^ unsupported cast
4+
LL | let u = 5 as bool; //~ ERROR cannot cast as `bool`
5+
| ^^^^^^^^^ help: compare with zero instead: `5 != 0`
6+
7+
error[E0054]: cannot cast as `bool`
8+
--> $DIR/cast-as-bool.rs:5:13
9+
|
10+
LL | let t = (1 + 2) as bool; //~ ERROR cannot cast as `bool`
11+
| ^^^^^^^^^^^^^^^ help: compare with zero instead: `(1 + 2) != 0`
12+
13+
error[E0054]: cannot cast as `bool`
14+
--> $DIR/cast-as-bool.rs:8:13
615
|
7-
= help: compare with zero instead
16+
LL | let v = "hello" as bool; //~ ERROR cannot cast as `bool`
17+
| ^^^^^^^^^^^^^^^ unsupported cast
818

9-
error: aborting due to previous error
19+
error: aborting due to 3 previous errors
1020

1121
For more information about this error, try `rustc --explain E0054`.

src/test/ui/cast/cast-rfc0401-2.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error[E0054]: cannot cast as `bool`
22
--> $DIR/cast-rfc0401-2.rs:6:13
33
|
44
LL | let _ = 3 as bool;
5-
| ^^^^^^^^^ unsupported cast
6-
|
7-
= help: compare with zero instead
5+
| ^^^^^^^^^ help: compare with zero instead: `3 != 0`
86

97
error: aborting due to previous error
108

src/test/ui/error-codes/E0054.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error[E0054]: cannot cast as `bool`
22
--> $DIR/E0054.rs:3:24
33
|
44
LL | let x_is_nonzero = x as bool; //~ ERROR E0054
5-
| ^^^^^^^^^ unsupported cast
6-
|
7-
= help: compare with zero instead
5+
| ^^^^^^^^^ help: compare with zero instead: `x != 0`
86

97
error: aborting due to previous error
108

src/test/ui/error-festival.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ error[E0054]: cannot cast as `bool`
5252
--> $DIR/error-festival.rs:33:24
5353
|
5454
LL | let x_is_nonzero = x as bool;
55-
| ^^^^^^^^^ unsupported cast
56-
|
57-
= help: compare with zero instead
55+
| ^^^^^^^^^ help: compare with zero instead: `x != 0`
5856

5957
error[E0606]: casting `&u8` as `u32` is invalid
6058
--> $DIR/error-festival.rs:37:18

src/test/ui/mismatched_types/cast-rfc0401.stderr

+1-5
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,13 @@ error[E0054]: cannot cast as `bool`
9090
--> $DIR/cast-rfc0401.rs:39:13
9191
|
9292
LL | let _ = 3_i32 as bool; //~ ERROR cannot cast
93-
| ^^^^^^^^^^^^^ unsupported cast
94-
|
95-
= help: compare with zero instead
93+
| ^^^^^^^^^^^^^ help: compare with zero instead: `3_i32 != 0`
9694

9795
error[E0054]: cannot cast as `bool`
9896
--> $DIR/cast-rfc0401.rs:40:13
9997
|
10098
LL | let _ = E::A as bool; //~ ERROR cannot cast
10199
| ^^^^^^^^^^^^ unsupported cast
102-
|
103-
= help: compare with zero instead
104100

105101
error[E0604]: only `u8` can be cast as `char`, not `u32`
106102
--> $DIR/cast-rfc0401.rs:41:13

0 commit comments

Comments
 (0)