]> granicus.if.org Git - clang/commitdiff
Warn when an expression result in a LabelStmt is unused.
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sun, 19 Sep 2010 21:21:10 +0000 (21:21 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sun, 19 Sep 2010 21:21:10 +0000 (21:21 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@114314 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/Expr.cpp
lib/Sema/SemaStmt.cpp
test/Sema/warn-unused-value.c

index 4c6822c041ff08aa72e13dd311be43511bb31793..b31ec2314dc12f8ae4860e7d0456644471be113c 100644 (file)
@@ -1225,9 +1225,13 @@ bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
     // however, if the result of the stmt expr is dead, we don't want to emit a
     // warning.
     const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
-    if (!CS->body_empty())
+    if (!CS->body_empty()) {
       if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
         return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
+      if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
+        if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
+          return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
+    }
 
     if (getType()->isVoidType())
       return false;
index 8ec12a460db6298983c2e948d9ea15a7f761cffa..47ea4f2c2535e124a81221f02fec277fb0926d0e 100644 (file)
@@ -67,6 +67,9 @@ void Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) {
 }
 
 void Sema::DiagnoseUnusedExprResult(const Stmt *S) {
+  if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S))
+    return DiagnoseUnusedExprResult(Label->getSubStmt());
+
   const Expr *E = dyn_cast_or_null<Expr>(S);
   if (!E)
     return;
index 1a7e745785b365cd070692a15c09ccac99c12369..7c36b6983e701dba2d989d4c97f6dfc9587ee9c4 100644 (file)
@@ -52,6 +52,9 @@ void pr4806() {
   volatile int* pj = &j;
   *pi;              // expected-warning {{expression result unused}}
   *pj;
+
+  foo_label:
+  i;                // expected-warning {{expression result unused}}
 }
 
 // Don't warn about unused '||', '&&' expressions that contain assignments.