Skip to content

Commit

Permalink
Auto merge of #43854 - estebank:missing-cond, r=nikomatsakis
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Aug 22, 2017
2 parents 942711e + f063233 commit 6722996
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,32 @@ pub struct Expr {
pub attrs: ThinVec<Attribute>
}

impl Expr {
/// Wether this expression would be valid somewhere that expects a value, for example, an `if`
/// condition.
pub fn returns(&self) -> bool {
if let ExprKind::Block(ref block) = self.node {
match block.stmts.last().map(|last_stmt| &last_stmt.node) {
// implicit return
Some(&StmtKind::Expr(_)) => true,
Some(&StmtKind::Semi(ref expr)) => {
if let ExprKind::Ret(_) = expr.node {
// last statement is explicit return
true
} else {
false
}
}
// This is a block that doesn't end in either an implicit or explicit return
_ => false,
}
} else {
// This is not a block, it is a value
true
}
}
}

impl fmt::Debug for Expr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "expr({}: {})", self.id, pprust::expr_to_string(self))
Expand Down
12 changes: 12 additions & 0 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions src/test/ui/issue-13483.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
if true {
} else if {
} else {
}
}

fn foo() {
if true {
} else if {
}
bar();
}

fn bar() {}
14 changes: 14 additions & 0 deletions src/test/ui/issue-13483.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: missing condition for `if` statemement
--> $DIR/issue-13483.rs:13:14
|
13 | } else if {
| ^ expected if condition here

error: missing condition for `if` statemement
--> $DIR/issue-13483.rs:20:14
|
20 | } else if {
| ^ expected if condition here

error: aborting due to 2 previous errors

0 comments on commit 6722996

Please sign in to comment.