Skip to content

Commit 20a2716

Browse files
committed
Check for else keyword on missing if condition
1 parent c4672f8 commit 20a2716

File tree

3 files changed

+12
-29
lines changed

3 files changed

+12
-29
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,7 +2130,7 @@ impl<'a> Parser<'a> {
21302130
return self.parse_lambda_expr(lo, CaptureBy::Value, attrs);
21312131
}
21322132
if self.eat_keyword(keywords::If) {
2133-
return self.parse_if_expr(attrs, false);
2133+
return self.parse_if_expr(attrs);
21342134
}
21352135
if self.eat_keyword(keywords::For) {
21362136
let lo = self.prev_span;
@@ -2962,25 +2962,20 @@ impl<'a> Parser<'a> {
29622962
}
29632963

29642964
/// Parse an 'if' or 'if let' expression ('if' token already eaten)
2965-
pub fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>,
2966-
in_else: bool) -> PResult<'a, P<Expr>> {
2965+
pub fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
29672966
if self.check_keyword(keywords::Let) {
29682967
return self.parse_if_let_expr(attrs);
29692968
}
29702969
let lo = self.prev_span;
29712970
let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL, None)?;
2972-
let thn = self.parse_block().map_err(|mut err| {
2973-
if in_else {
2974-
err.cancel();
2975-
let sp = lo.next_point();
2976-
let mut err = self.diagnostic()
2977-
.struct_span_err(sp, "missing condition for `if` statemement");
2978-
err.span_label(sp, "expected if condition here");
2979-
err
2980-
} else {
2981-
err
2982-
}
2983-
})?;
2971+
if self.eat_keyword(keywords::Else) {
2972+
let sp = lo.next_point();
2973+
let mut err = self.diagnostic()
2974+
.struct_span_err(sp, "missing condition for `if` statemement");
2975+
err.span_label(sp, "expected if condition here");
2976+
return Err(err)
2977+
}
2978+
let thn = self.parse_block()?;
29842979
let mut els: Option<P<Expr>> = None;
29852980
let mut hi = thn.span;
29862981
if self.eat_keyword(keywords::Else) {
@@ -3037,7 +3032,7 @@ impl<'a> Parser<'a> {
30373032
// `else` token already eaten
30383033
pub fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
30393034
if self.eat_keyword(keywords::If) {
3040-
return self.parse_if_expr(ThinVec::new(), true);
3035+
return self.parse_if_expr(ThinVec::new());
30413036
} else {
30423037
let blk = self.parse_block()?;
30433038
return Ok(self.mk_expr(blk.span, ExprKind::Block(blk), ThinVec::new()));

src/test/ui/issue-13483.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,3 @@ fn main() {
1414
} else {
1515
};
1616
}
17-
18-
fn foo() {
19-
if true {
20-
} else if { //ERROR: MISSING CONDITIONAL
21-
};
22-
}

src/test/ui/issue-13483.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@ error: missing condition for `if` statemement
44
13 | } else if { //ERROR: MISSING CONDITIONAL
55
| ^ expected if condition here
66

7-
error: missing conditional
8-
--> $DIR/issue-13483.rs:20:14
9-
|
10-
20 | } else if { //ERROR: MISSING CONDITIONAL
11-
| ^ expected if condition here
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
148

0 commit comments

Comments
 (0)