Skip to content

Commit

Permalink
Rollup merge of #97370 - compiler-errors:else-no-if-2, r=Dylan-DPC
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
Dylan-DPC authored May 25, 2022
2 parents ca269b1 + d61d30d commit fe727e4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 18 deletions.
9 changes: 1 addition & 8 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2291,16 +2291,9 @@ impl<'a> Parser<'a> {
.span_label(else_span, "expected an `if` or a block after this `else`")
.span_suggestion(
cond.span.shrink_to_lo(),
"add an `if` if this is the condition to an chained `if` statement after the `else`",
"add an `if` if this is the condition of a chained `else if` statement",
"if ".to_string(),
Applicability::MaybeIncorrect,
).multipart_suggestion(
"... otherwise, place this expression inside of a block if it is not an `if` condition",
vec![
(cond.span.shrink_to_lo(), "{ ".to_string()),
(cond.span.shrink_to_hi(), " }".to_string()),
],
Applicability::MaybeIncorrect,
)
.emit();
self.parse_if_after_cond(AttrVec::new(), cond.span.shrink_to_lo(), cond)?
Expand Down
12 changes: 2 additions & 10 deletions src/test/ui/parser/else-no-if.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ LL | } else false {
| |
| expected an `if` or a block after this `else`
|
help: add an `if` if this is the condition to an chained `if` statement after the `else`
help: add an `if` if this is the condition of a chained `else if` statement
|
LL | } else if false {
| ++
help: ... otherwise, place this expression inside of a block if it is not an `if` condition
|
LL | } else { false } {
| + +

error: expected `{`, found `falsy`
--> $DIR/else-no-if.rs:10:12
Expand All @@ -23,14 +19,10 @@ LL | } else falsy() {
| |
| expected an `if` or a block after this `else`
|
help: add an `if` if this is the condition to an chained `if` statement after the `else`
help: add an `if` if this is the condition of a chained `else if` statement
|
LL | } else if falsy() {
| ++
help: ... otherwise, place this expression inside of a block if it is not an `if` condition
|
LL | } else { falsy() } {
| + +

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

0 comments on commit fe727e4

Please sign in to comment.