r298853 - Look through CXXBindTemporaryExprs when checking CXXFunctionCastExprs

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 27 09:29:42 PDT 2017


Author: djasper
Date: Mon Mar 27 11:29:41 2017
New Revision: 298853

URL: http://llvm.org/viewvc/llvm-project?rev=298853&view=rev
Log:
Look through CXXBindTemporaryExprs when checking CXXFunctionCastExprs
for unused values.

This fixes a regression caused by r298676, where constructor calls to
classes with non-trivial dtor were marked as unused if the first
argument is an initializer list. This is inconsistent (as the test
shows) and also warns on a reasonbly common code pattern where people
just call constructors to create and immediately destroy an object.

Modified:
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/SemaCXX/warn-unused-value.cpp

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=298853&r1=298852&r2=298853&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Mon Mar 27 11:29:41 2017
@@ -290,9 +290,15 @@ void Sema::DiagnoseUnusedExprResult(cons
       DiagID = diag::warn_unused_property_expr;
   } else if (const CXXFunctionalCastExpr *FC
                                        = dyn_cast<CXXFunctionalCastExpr>(E)) {
-    if (isa<CXXConstructExpr>(FC->getSubExpr()) ||
-        isa<CXXTemporaryObjectExpr>(FC->getSubExpr()))
+    const Expr *E = FC->getSubExpr();
+    if (const CXXBindTemporaryExpr *TE = dyn_cast<CXXBindTemporaryExpr>(E))
+      E = TE->getSubExpr();
+    if (isa<CXXTemporaryObjectExpr>(E))
       return;
+    if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E))
+      if (const CXXRecordDecl *RD = CE->getType()->getAsCXXRecordDecl())
+        if (!RD->getAttr<WarnUnusedAttr>())
+          return;
   }
   // Diagnose "(void*) blah" as a typo for "(void) blah".
   else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) {

Modified: cfe/trunk/test/SemaCXX/warn-unused-value.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-value.cpp?rev=298853&r1=298852&r2=298853&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-value.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unused-value.cpp Mon Mar 27 11:29:41 2017
@@ -59,11 +59,13 @@ struct Used {
   Used();
   Used(int);
   Used(int, int);
+  ~Used() {}
 };
 struct __attribute__((warn_unused)) Unused {
   Unused();
   Unused(int);
   Unused(int, int);
+  ~Unused() {}
 };
 void f() {
   Used();
@@ -72,6 +74,10 @@ void f() {
   Unused();     // expected-warning {{expression result unused}}
   Unused(1);    // expected-warning {{expression result unused}}
   Unused(1, 1); // expected-warning {{expression result unused}}
+#if __cplusplus >= 201103L // C++11 or later
+  Used({});
+  Unused({}); // expected-warning {{expression result unused}}
+#endif
 }
 }
 




More information about the cfe-commits mailing list