From 75f794b36977628281921472eaf4bc63d9cb336c 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@298853 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaStmt.cpp | 10 ++++++++-- test/SemaCXX/warn-unused-value.cpp | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index f393e3e794..4cf1ad9e1c 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/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/test/SemaCXX/warn-unused-value.cpp b/test/SemaCXX/warn-unused-value.cpp index d6ec0fb5d1..98e2a4e863 100644 --- a/test/SemaCXX/warn-unused-value.cpp +++ b/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.40.0