Skip to content

Commit f063233

Browse files
committed
Verify that an if condition block returns a value
1 parent 20a2716 commit f063233

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

src/libsyntax/ast.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,32 @@ pub struct Expr {
844844
pub attrs: ThinVec<Attribute>
845845
}
846846

847+
impl Expr {
848+
/// Wether this expression would be valid somewhere that expects a value, for example, an `if`
849+
/// condition.
850+
pub fn returns(&self) -> bool {
851+
if let ExprKind::Block(ref block) = self.node {
852+
match block.stmts.last().map(|last_stmt| &last_stmt.node) {
853+
// implicit return
854+
Some(&StmtKind::Expr(_)) => true,
855+
Some(&StmtKind::Semi(ref expr)) => {
856+
if let ExprKind::Ret(_) = expr.node {
857+
// last statement is explicit return
858+
true
859+
} else {
860+
false
861+
}
862+
}
863+
// This is a block that doesn't end in either an implicit or explicit return
864+
_ => false,
865+
}
866+
} else {
867+
// This is not a block, it is a value
868+
true
869+
}
870+
}
871+
}
872+
847873
impl fmt::Debug for Expr {
848874
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
849875
write!(f, "expr({}: {})", self.id, pprust::expr_to_string(self))

src/libsyntax/parse/parser.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2968,7 +2968,12 @@ impl<'a> Parser<'a> {
29682968
}
29692969
let lo = self.prev_span;
29702970
let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL, None)?;
2971-
if self.eat_keyword(keywords::Else) {
2971+
2972+
// Verify that the parsed `if` condition makes sense as a condition. If it is a block, then
2973+
// verify that the last statement is either an implicit return (no `;`) or an explicit
2974+
// return. This won't catch blocks with an explicit `return`, but that would be caught by
2975+
// the dead code lint.
2976+
if self.eat_keyword(keywords::Else) || !cond.returns() {
29722977
let sp = lo.next_point();
29732978
let mut err = self.diagnostic()
29742979
.struct_span_err(sp, "missing condition for `if` statemement");

src/test/ui/issue-13483.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@
1010

1111
fn main() {
1212
if true {
13-
} else if { //ERROR: MISSING CONDITIONAL
13+
} else if {
1414
} else {
15-
};
15+
}
1616
}
17+
18+
fn foo() {
19+
if true {
20+
} else if {
21+
}
22+
bar();
23+
}
24+
25+
fn bar() {}

src/test/ui/issue-13483.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
error: missing condition for `if` statemement
22
--> $DIR/issue-13483.rs:13:14
33
|
4-
13 | } else if { //ERROR: MISSING CONDITIONAL
4+
13 | } else if {
55
| ^ expected if condition here
66

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

0 commit comments

Comments
 (0)