From f06323337dd0cf748e8a0ca49d91dac2eae6b4a9 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Thu, 17 Aug 2017 16:51:52 -0700 Subject: Verify that an `if` condition block returns a value --- src/libsyntax/parse/parser.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 22999afb48e..85d2a485bfa 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2968,7 +2968,12 @@ impl<'a> Parser<'a> { } let lo = self.prev_span; let cond = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL, None)?; - if self.eat_keyword(keywords::Else) { + + // 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"); -- cgit 1.4.1-3-g733a5