Skip to content

Commit c4672f8

Browse files
committed
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 { | ^^^^ ```
1 parent a80a873 commit c4672f8

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 16 additions & 4 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);
2133+
return self.parse_if_expr(attrs, false);
21342134
}
21352135
if self.eat_keyword(keywords::For) {
21362136
let lo = self.prev_span;
@@ -2962,13 +2962,25 @@ 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>) -> PResult<'a, P<Expr>> {
2965+
pub fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>,
2966+
in_else: bool) -> PResult<'a, P<Expr>> {
29662967
if self.check_keyword(keywords::Let) {
29672968
return self.parse_if_let_expr(attrs);
29682969
}
29692970
let lo = self.prev_span;
29702971
let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL, None)?;
2971-
let thn = self.parse_block()?;
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+
})?;
29722984
let mut els: Option<P<Expr>> = None;
29732985
let mut hi = thn.span;
29742986
if self.eat_keyword(keywords::Else) {
@@ -3025,7 +3037,7 @@ impl<'a> Parser<'a> {
30253037
// `else` token already eaten
30263038
pub fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
30273039
if self.eat_keyword(keywords::If) {
3028-
return self.parse_if_expr(ThinVec::new());
3040+
return self.parse_if_expr(ThinVec::new(), true);
30293041
} else {
30303042
let blk = self.parse_block()?;
30313043
return Ok(self.mk_expr(blk.span, ExprKind::Block(blk), ThinVec::new()));

src/test/ui/issue-13483.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
if true {
13+
} else if { //ERROR: MISSING CONDITIONAL
14+
} else {
15+
};
16+
}
17+
18+
fn foo() {
19+
if true {
20+
} else if { //ERROR: MISSING CONDITIONAL
21+
};
22+
}

src/test/ui/issue-13483.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: missing condition for `if` statemement
2+
--> $DIR/issue-13483.rs:13:14
3+
|
4+
13 | } else if { //ERROR: MISSING CONDITIONAL
5+
| ^ expected if condition here
6+
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
14+

0 commit comments

Comments
 (0)