Auto merge of #43854 - estebank:missing-cond, r=nikomatsakis
Point out missing if conditional
On a case where an else conditional is missing, point this out
instead of the token immediately after the (incorrect) else block:
```
error: missing condition for `if` statemementt push fork -f
--> $DIR/issue-13483.rs:16:5
|
13 | } else if {
| ^ expected if condition here
```
instead of
```
error: expected `{`, found `else`
--> ../../src/test/ui/issue-13483.rs:14:7
|
14 | } else {
| ^^^^
```
Fix #13483.
This commit is contained in:
@@ -2965,6 +2965,18 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
let lo = self.prev_span;
|
||||
let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL, None)?;
|
||||
|
||||
// Verify that the parsed `if` condition makes sense as a condition. If it is a block, then
|
||||
// verify that the last statement is either an implicit return (no `;`) or an explicit
|
||||
// return. This won't catch blocks with an explicit `return`, but that would be caught by
|
||||
// the dead code lint.
|
||||
if self.eat_keyword(keywords::Else) || !cond.returns() {
|
||||
let sp = lo.next_point();
|
||||
let mut err = self.diagnostic()
|
||||
.struct_span_err(sp, "missing condition for `if` statemement");
|
||||
err.span_label(sp, "expected if condition here");
|
||||
return Err(err)
|
||||
}
|
||||
let thn = self.parse_block()?;
|
||||
let mut els: Option<P<Expr>> = None;
|
||||
let mut hi = thn.span;
|
||||
|
||||
Reference in New Issue
Block a user