if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
// White-list anything with an __attribute__((unused)) type.
- QualType Ty = VD->getType();
+ const auto *Ty = VD->getType().getTypePtr();
// Only look at the outermost level of typedef.
if (const TypedefType *TT = Ty->getAs<TypedefType>()) {
if (Ty->isIncompleteType() || Ty->isDependentType())
return false;
+ // Look at the element type to ensure that the warning behaviour is
+ // consistent for both scalars and arrays.
+ Ty = Ty->getBaseElementTypeUnsafe();
+
if (const TagType *TT = Ty->getAs<TagType>()) {
const TagDecl *Tag = TT->getDecl();
if (Tag->hasAttr<UnusedAttr>())
}
#include "Inputs/warn-unused-variables.h"
+
+namespace arrayRecords {
+
+int total = 0;
+
+class Adder {
+public:
+ Adder(int x); // out of line below
+ ~Adder() {}
+};
+
+Adder::Adder(int x) {
+ total += x;
+}
+
+struct Foo {
+ int x;
+ Foo(int x) : x(x) {}
+};
+
+struct S1 {
+ S1();
+};
+
+void foo(int size) {
+ S1 y; // no warning
+ S1 yarray[2]; // no warning
+ S1 dynArray[size]; // no warning
+ S1 nestedArray[1][2][3]; // no warning
+
+ Adder scalerInFuncScope = 134; // no warning
+ Adder arrayInFuncScope[] = { 135, 136 }; // no warning
+ Adder nestedArrayInFuncScope[2][2] = { {1,2}, {3,4} }; // no warning
+
+ Foo fooScalar = 1; // expected-warning {{unused variable 'fooScalar'}}
+ Foo fooArray[] = {1,2}; // expected-warning {{unused variable 'fooArray'}}
+ Foo fooNested[2][2] = { {1,2}, {3,4} }; // expected-warning {{unused variable 'fooNested'}}
+}
+
+template<int N>
+void bar() {
+ Adder scaler = 123; // no warning
+ Adder array[N] = {1,2}; // no warning
+}
+
+void test() {
+ foo(10);
+ bar<2>();
+}
+
+}