Skip to content

Commit fe727e4

Browse files
authored
Rollup merge of #97370 - compiler-errors:else-no-if-2, r=Dylan-DPC
Minor improvement on else-no-if diagnostic Don't suggest wrapping in block since it's highly likely to be a missing `if` after `else`. Also rework message a bit (open to further suggestions). cc: #97298 (comment) r? `@estebank`
2 parents ca269b1 + d61d30d commit fe727e4

File tree

2 files changed

+3
-18
lines changed

2 files changed

+3
-18
lines changed

compiler/rustc_parse/src/parser/expr.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -2291,16 +2291,9 @@ impl<'a> Parser<'a> {
22912291
.span_label(else_span, "expected an `if` or a block after this `else`")
22922292
.span_suggestion(
22932293
cond.span.shrink_to_lo(),
2294-
"add an `if` if this is the condition to an chained `if` statement after the `else`",
2294+
"add an `if` if this is the condition of a chained `else if` statement",
22952295
"if ".to_string(),
22962296
Applicability::MaybeIncorrect,
2297-
).multipart_suggestion(
2298-
"... otherwise, place this expression inside of a block if it is not an `if` condition",
2299-
vec![
2300-
(cond.span.shrink_to_lo(), "{ ".to_string()),
2301-
(cond.span.shrink_to_hi(), " }".to_string()),
2302-
],
2303-
Applicability::MaybeIncorrect,
23042297
)
23052298
.emit();
23062299
self.parse_if_after_cond(AttrVec::new(), cond.span.shrink_to_lo(), cond)?

src/test/ui/parser/else-no-if.stderr

+2-10
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ LL | } else false {
66
| |
77
| expected an `if` or a block after this `else`
88
|
9-
help: add an `if` if this is the condition to an chained `if` statement after the `else`
9+
help: add an `if` if this is the condition of a chained `else if` statement
1010
|
1111
LL | } else if false {
1212
| ++
13-
help: ... otherwise, place this expression inside of a block if it is not an `if` condition
14-
|
15-
LL | } else { false } {
16-
| + +
1713

1814
error: expected `{`, found `falsy`
1915
--> $DIR/else-no-if.rs:10:12
@@ -23,14 +19,10 @@ LL | } else falsy() {
2319
| |
2420
| expected an `if` or a block after this `else`
2521
|
26-
help: add an `if` if this is the condition to an chained `if` statement after the `else`
22+
help: add an `if` if this is the condition of a chained `else if` statement
2723
|
2824
LL | } else if falsy() {
2925
| ++
30-
help: ... otherwise, place this expression inside of a block if it is not an `if` condition
31-
|
32-
LL | } else { falsy() } {
33-
| + +
3426

3527
error: expected `{`, found `falsy`
3628
--> $DIR/else-no-if.rs:17:12

0 commit comments

Comments
 (0)