]> granicus.if.org Git - clang/commitdiff
Diagnose unused expression results for all statements, just not compound statements.
authorAnders Carlsson <andersca@mac.com>
Thu, 30 Jul 2009 22:39:03 +0000 (22:39 +0000)
committerAnders Carlsson <andersca@mac.com>
Thu, 30 Jul 2009 22:39:03 +0000 (22:39 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@77631 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaStmt.cpp
test/Sema/unused-expr.c

index badeba79ceabda2eca2a6cbe35a87813a45c7eaa..f00d60089b23416036527a6f02b591f5f71205c8 100644 (file)
@@ -52,7 +52,7 @@ Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg,
 }
 
 void Sema::DiagnoseUnusedExprResult(const Stmt *S) {
-  const Expr *E = dyn_cast<Expr>(S);
+  const Expr *E = dyn_cast_or_null<Expr>(S);
   if (!E)
     return;
 
@@ -215,6 +215,7 @@ Sema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal,
   }
 
   Stmt *thenStmt = ThenVal.takeAs<Stmt>();
+  DiagnoseUnusedExprResult(thenStmt);
 
   // Warn if the if block has a null body without an else value.
   // this helps prevent bugs due to typos, such as
@@ -225,9 +226,12 @@ Sema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal,
       Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
   }
 
+  Stmt *elseStmt = ElseVal.takeAs<Stmt>();
+  DiagnoseUnusedExprResult(elseStmt);
+  
   CondResult.release();
   return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt,
-                                    ElseLoc, ElseVal.takeAs<Stmt>()));
+                                    ElseLoc, elseStmt));
 }
 
 Action::OwningStmtResult
@@ -571,9 +575,11 @@ Sema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond, StmtArg Body) {
                        << condType << condExpr->getSourceRange());
   }
 
+  Stmt *bodyStmt = Body.takeAs<Stmt>();
+  DiagnoseUnusedExprResult(bodyStmt);
+  
   CondArg.release();
-  return Owned(new (Context) WhileStmt(condExpr, Body.takeAs<Stmt>(), 
-                                       WhileLoc));
+  return Owned(new (Context) WhileStmt(condExpr, bodyStmt, WhileLoc));
 }
 
 Action::OwningStmtResult
@@ -597,8 +603,11 @@ Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body,
                        << condType << condExpr->getSourceRange());
   }
 
+  Stmt *bodyStmt = Body.takeAs<Stmt>();
+  DiagnoseUnusedExprResult(bodyStmt);
+
   Cond.release();
-  return Owned(new (Context) DoStmt(Body.takeAs<Stmt>(), condExpr, DoLoc,
+  return Owned(new (Context) DoStmt(bodyStmt, condExpr, DoLoc,
                                     WhileLoc, CondRParen));
 }
 
@@ -639,6 +648,9 @@ Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
                             diag::err_typecheck_statement_requires_scalar)
         << SecondType << Second->getSourceRange());
   }
+  
+  DiagnoseUnusedExprResult(Body);
+
   first.release();
   second.release();
   third.release();
index a5e5ec4e1491c42138cf35fb01a2fde7d1e9a39f..c55afeadc45a78640d2fe12e262e21034e8b057a 100644 (file)
@@ -50,4 +50,23 @@ void nowarn(unsigned char* a, unsigned char* b)
   ((void)0), y = x;
 }
 
+void t4(int a) {
+  int b = 0;
+
+  if (a)
+    b == 1; // expected-warning{{expression result unused}}
+  else
+    b == 2; // expected-warning{{expression result unused}}
+    
+  while (1)
+    b == 3; // expected-warning{{expression result unused}}
+
+  do
+    b == 4; // expected-warning{{expression result unused}}
+  while (1);
+  
+  for (;;)
+    b == 5; // expected-warning{{expression result unused}}
+}
+