Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit cb0b661

Browse files
committedOct 14, 2017
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
1 parent 9541b49 commit cb0b661

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed
 

‎lib/Sema/SemaDecl.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,8 @@ static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) {
17231723
dyn_cast<CXXConstructExpr>(Init);
17241724
if (Construct && !Construct->isElidable()) {
17251725
CXXConstructorDecl *CD = Construct->getConstructor();
1726-
if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>())
1726+
if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>() &&
1727+
!VD->evaluateValue())
17271728
return false;
17281729
}
17291730
}

‎test/SemaCXX/warn-unused-variables.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify %s
2+
// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=c++11 %s
23
template<typename T> void f() {
34
T t;
45
t = 17;
@@ -194,3 +195,35 @@ void test() {
194195
}
195196

196197
}
198+
199+
#if __cplusplus >= 201103L
200+
namespace with_constexpr {
201+
template <typename T>
202+
struct Literal {
203+
T i;
204+
Literal() = default;
205+
constexpr Literal(T i) : i(i) {}
206+
};
207+
208+
struct NoLiteral {
209+
int i;
210+
NoLiteral() = default;
211+
constexpr NoLiteral(int i) : i(i) {}
212+
~NoLiteral() {}
213+
};
214+
215+
static Literal<int> gl1; // expected-warning {{unused variable 'gl1'}}
216+
static Literal<int> gl2(1); // expected-warning {{unused variable 'gl2'}}
217+
static const Literal<int> gl3(0); // expected-warning {{unused variable 'gl3'}}
218+
219+
template <typename T>
220+
void test(int i) {
221+
Literal<int> l1; // expected-warning {{unused variable 'l1'}}
222+
Literal<int> l2(42); // expected-warning {{unused variable 'l2'}}
223+
Literal<int> l3(i); // no-warning
224+
Literal<T> l4(0); // no-warning
225+
NoLiteral nl1; // no-warning
226+
NoLiteral nl2(42); // no-warning
227+
}
228+
}
229+
#endif

0 commit comments

Comments
 (0)