From: Benjamin Kramer Date: Sat, 14 Oct 2017 15:59:34 +0000 (+0000) Subject: Re-land r315787, "[Sema] Warn about unused variables if we can constant evaluate... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cb0b66133f560ff830c1ee82c117476a246f487d;p=clang Re-land r315787, "[Sema] Warn about unused variables if we can constant evaluate the initializer." The warnings in libc++ tests were fixed in the meantime. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@315811 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 31abdfa3f6..e6f3aeae96 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1723,7 +1723,8 @@ static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { dyn_cast(Init); if (Construct && !Construct->isElidable()) { CXXConstructorDecl *CD = Construct->getConstructor(); - if (!CD->isTrivial() && !RD->hasAttr()) + if (!CD->isTrivial() && !RD->hasAttr() && + !VD->evaluateValue()) return false; } } diff --git a/test/SemaCXX/warn-unused-variables.cpp b/test/SemaCXX/warn-unused-variables.cpp index d7be785b35..0974cf5a61 100644 --- a/test/SemaCXX/warn-unused-variables.cpp +++ b/test/SemaCXX/warn-unused-variables.cpp @@ -1,4 +1,5 @@ // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify %s +// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=c++11 %s template void f() { T t; t = 17; @@ -194,3 +195,35 @@ void test() { } } + +#if __cplusplus >= 201103L +namespace with_constexpr { +template +struct Literal { + T i; + Literal() = default; + constexpr Literal(T i) : i(i) {} +}; + +struct NoLiteral { + int i; + NoLiteral() = default; + constexpr NoLiteral(int i) : i(i) {} + ~NoLiteral() {} +}; + +static Literal gl1; // expected-warning {{unused variable 'gl1'}} +static Literal gl2(1); // expected-warning {{unused variable 'gl2'}} +static const Literal gl3(0); // expected-warning {{unused variable 'gl3'}} + +template +void test(int i) { + Literal l1; // expected-warning {{unused variable 'l1'}} + Literal l2(42); // expected-warning {{unused variable 'l2'}} + Literal l3(i); // no-warning + Literal l4(0); // no-warning + NoLiteral nl1; // no-warning + NoLiteral nl2(42); // no-warning +} +} +#endif