Skip to content

Commit 6722996

Browse files
committed
Auto merge of rust-lang#43854 - estebank:missing-cond, r=nikomatsakis
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 rust-lang#13483.
2 parents 942711e + f063233 commit 6722996

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,6 +2965,18 @@ impl<'a> Parser<'a> {
29652965
}
29662966
let lo = self.prev_span;
29672967
let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL, None)?;
2968+
2969+
// Verify that the parsed `if` condition makes sense as a condition. If it is a block, then
2970+
// verify that the last statement is either an implicit return (no `;`) or an explicit
2971+
// return. This won't catch blocks with an explicit `return`, but that would be caught by
2972+
// the dead code lint.
2973+
if self.eat_keyword(keywords::Else) || !cond.returns() {
2974+
let sp = lo.next_point();
2975+
let mut err = self.diagnostic()
2976+
.struct_span_err(sp, "missing condition for `if` statemement");
2977+
err.span_label(sp, "expected if condition here");
2978+
return Err(err)
2979+
}
29682980
let thn = self.parse_block()?;
29692981
let mut els: Option<P<Expr>> = None;
29702982
let mut hi = thn.span;

src/test/ui/issue-13483.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 {
14+
} else {
15+
}
16+
}
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: 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 {
5+
| ^ expected if condition here
6+
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
14+

0 commit comments

Comments
 (0)