From: Reid Kleckner Date: Thu, 16 Nov 2017 21:26:18 +0000 (+0000) Subject: Issue -Wempty-body warnings for else blocks X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b7ee0571e3c58a921e7e9772da2525d7ce2ad186;p=clang Issue -Wempty-body warnings for else blocks This looks like it was just an oversight. Fixes http://llvm.org/pr35319 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@318456 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index c27b495168..4542fc9194 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -8090,6 +8090,8 @@ def err_switch_incomplete_class_type : Error< def warn_empty_if_body : Warning< "if statement has empty body">, InGroup; +def warn_empty_else_body : Warning< + "else clause has empty body">, InGroup; def warn_empty_for_body : Warning< "for loop has empty body">, InGroup; def warn_empty_range_based_for_body : Warning< diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 3a3eb5e7b5..07b70305c1 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -527,7 +527,9 @@ Sema::ActOnIfStmt(SourceLocation IfLoc, bool IsConstexpr, Stmt *InitStmt, CondExpr->getExprLoc())) CommaVisitor(*this).Visit(CondExpr); - if (!elseStmt) + if (elseStmt) + DiagnoseEmptyStmtBody(ElseLoc, elseStmt, diag::warn_empty_else_body); + else DiagnoseEmptyStmtBody(CondExpr->getLocEnd(), thenStmt, diag::warn_empty_if_body); diff --git a/test/SemaCXX/warn-empty-body.cpp b/test/SemaCXX/warn-empty-body.cpp index a248c4251d..bd6b47f053 100644 --- a/test/SemaCXX/warn-empty-body.cpp +++ b/test/SemaCXX/warn-empty-body.cpp @@ -238,6 +238,26 @@ void test6(int x, int y) { } } +void test_if_else(int x) { + if (x); // expected-warning{{if statement has empty body}} expected-note{{separate line}} + + if (x) + ; // no-warning + + if (x) + ; // no-warning + else + ; // no-warning + + if (x) + ; // no-warning + else; // expected-warning{{else clause has empty body}} expected-note{{separate line}} + + if (x) + ; // no-warning + else EMPTY(x); // no-warning +} + void test_errors(int x) { if (1) aa; // expected-error{{use of undeclared identifier}}