]> granicus.if.org Git - clang/commitdiff
Issue -Wempty-body warnings for else blocks
authorReid Kleckner <rnk@google.com>
Thu, 16 Nov 2017 21:26:18 +0000 (21:26 +0000)
committerReid Kleckner <rnk@google.com>
Thu, 16 Nov 2017 21:26:18 +0000 (21:26 +0000)
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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaStmt.cpp
test/SemaCXX/warn-empty-body.cpp

index c27b495168768f9ef52aa6eb38fb87397108bf06..4542fc9194067a37509ed14d2bbd5985ed836d57 100644 (file)
@@ -8090,6 +8090,8 @@ def err_switch_incomplete_class_type : Error<
 
 def warn_empty_if_body : Warning<
   "if statement has empty body">, InGroup<EmptyBody>;
+def warn_empty_else_body : Warning<
+  "else clause has empty body">, InGroup<EmptyBody>;
 def warn_empty_for_body : Warning<
   "for loop has empty body">, InGroup<EmptyBody>;
 def warn_empty_range_based_for_body : Warning<
index 3a3eb5e7b5e12c77241c0f1a5a24225c62ea458f..07b70305c196cefb5bc2db00c1477eb654b0fa57 100644 (file)
@@ -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);
 
index a248c4251d5250c7047534ec1e8872b10978a259..bd6b47f053f1a3faa5beda718d0959bab6151005 100644 (file)
@@ -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}}