Check for else
keyword on missing if
condition
This commit is contained in:
parent
c4672f8e87
commit
20a2716206
3 changed files with 12 additions and 29 deletions
|
@ -2130,7 +2130,7 @@ impl<'a> Parser<'a> {
|
||||||
return self.parse_lambda_expr(lo, CaptureBy::Value, attrs);
|
return self.parse_lambda_expr(lo, CaptureBy::Value, attrs);
|
||||||
}
|
}
|
||||||
if self.eat_keyword(keywords::If) {
|
if self.eat_keyword(keywords::If) {
|
||||||
return self.parse_if_expr(attrs, false);
|
return self.parse_if_expr(attrs);
|
||||||
}
|
}
|
||||||
if self.eat_keyword(keywords::For) {
|
if self.eat_keyword(keywords::For) {
|
||||||
let lo = self.prev_span;
|
let lo = self.prev_span;
|
||||||
|
@ -2962,25 +2962,20 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an 'if' or 'if let' expression ('if' token already eaten)
|
/// Parse an 'if' or 'if let' expression ('if' token already eaten)
|
||||||
pub fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>,
|
pub fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
|
||||||
in_else: bool) -> PResult<'a, P<Expr>> {
|
|
||||||
if self.check_keyword(keywords::Let) {
|
if self.check_keyword(keywords::Let) {
|
||||||
return self.parse_if_let_expr(attrs);
|
return self.parse_if_let_expr(attrs);
|
||||||
}
|
}
|
||||||
let lo = self.prev_span;
|
let lo = self.prev_span;
|
||||||
let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL, None)?;
|
let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL, None)?;
|
||||||
let thn = self.parse_block().map_err(|mut err| {
|
if self.eat_keyword(keywords::Else) {
|
||||||
if in_else {
|
let sp = lo.next_point();
|
||||||
err.cancel();
|
let mut err = self.diagnostic()
|
||||||
let sp = lo.next_point();
|
.struct_span_err(sp, "missing condition for `if` statemement");
|
||||||
let mut err = self.diagnostic()
|
err.span_label(sp, "expected if condition here");
|
||||||
.struct_span_err(sp, "missing condition for `if` statemement");
|
return Err(err)
|
||||||
err.span_label(sp, "expected if condition here");
|
}
|
||||||
err
|
let thn = self.parse_block()?;
|
||||||
} else {
|
|
||||||
err
|
|
||||||
}
|
|
||||||
})?;
|
|
||||||
let mut els: Option<P<Expr>> = None;
|
let mut els: Option<P<Expr>> = None;
|
||||||
let mut hi = thn.span;
|
let mut hi = thn.span;
|
||||||
if self.eat_keyword(keywords::Else) {
|
if self.eat_keyword(keywords::Else) {
|
||||||
|
@ -3037,7 +3032,7 @@ impl<'a> Parser<'a> {
|
||||||
// `else` token already eaten
|
// `else` token already eaten
|
||||||
pub fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
|
pub fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
|
||||||
if self.eat_keyword(keywords::If) {
|
if self.eat_keyword(keywords::If) {
|
||||||
return self.parse_if_expr(ThinVec::new(), true);
|
return self.parse_if_expr(ThinVec::new());
|
||||||
} else {
|
} else {
|
||||||
let blk = self.parse_block()?;
|
let blk = self.parse_block()?;
|
||||||
return Ok(self.mk_expr(blk.span, ExprKind::Block(blk), ThinVec::new()));
|
return Ok(self.mk_expr(blk.span, ExprKind::Block(blk), ThinVec::new()));
|
||||||
|
|
|
@ -14,9 +14,3 @@ fn main() {
|
||||||
} else {
|
} else {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn foo() {
|
|
||||||
if true {
|
|
||||||
} else if { //ERROR: MISSING CONDITIONAL
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,11 +4,5 @@ error: missing condition for `if` statemement
|
||||||
13 | } else if { //ERROR: MISSING CONDITIONAL
|
13 | } else if { //ERROR: MISSING CONDITIONAL
|
||||||
| ^ expected if condition here
|
| ^ expected if condition here
|
||||||
|
|
||||||
error: missing conditional
|
error: aborting due to previous error
|
||||||
--> $DIR/issue-13483.rs:20:14
|
|
||||||
|
|
|
||||||
20 | } else if { //ERROR: MISSING CONDITIONAL
|
|
||||||
| ^ expected if condition here
|
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue