From 9c81a727eba60e6eece36e6e25ae76748c5f2a49 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 27 Mar 2017 16:29:41 +0000 Subject: [PATCH] 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. llvm-svn: 298853 --- clang/lib/Sema/SemaStmt.cpp | 10 ++++++++-- clang/test/SemaCXX/warn-unused-value.cpp | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index f393e3e..4cf1ad9 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -290,9 +290,15 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S) { DiagID = diag::warn_unused_property_expr; } else if (const CXXFunctionalCastExpr *FC = dyn_cast(E)) { - if (isa(FC->getSubExpr()) || - isa(FC->getSubExpr())) + const Expr *E = FC->getSubExpr(); + if (const CXXBindTemporaryExpr *TE = dyn_cast(E)) + E = TE->getSubExpr(); + if (isa(E)) return; + if (const CXXConstructExpr *CE = dyn_cast(E)) + if (const CXXRecordDecl *RD = CE->getType()->getAsCXXRecordDecl()) + if (!RD->getAttr()) + return; } // Diagnose "(void*) blah" as a typo for "(void) blah". else if (const CStyleCastExpr *CE = dyn_cast(E)) { diff --git a/clang/test/SemaCXX/warn-unused-value.cpp b/clang/test/SemaCXX/warn-unused-value.cpp index d6ec0fb..98e2a4e 100644 --- a/clang/test/SemaCXX/warn-unused-value.cpp +++ b/clang/test/SemaCXX/warn-unused-value.cpp @@ -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 } } -- 2.7.4