]> granicus.if.org Git - clang/commitdiff
Fix PR10168: don't warn for unused non-dependent variables in both the template defin...
authorRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 21 Jun 2011 23:42:09 +0000 (23:42 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 21 Jun 2011 23:42:09 +0000 (23:42 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@133580 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaTemplateInstantiateDecl.cpp
test/SemaCXX/for-range-unused.cpp
test/SemaCXX/warn-unused-variables.cpp

index e78aa2991e74a1a97ecbd83612e39614d16b57e7..cc66ec67593c8c67add5f55e3618902039e0b1ed 100644 (file)
@@ -415,8 +415,10 @@ Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
              !Var->isCXXForRangeDecl())
     SemaRef.ActOnUninitializedDecl(Var, false);
 
-  // Diagnose unused local variables.
-  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
+  // Diagnose unused local variables with dependent types, where the diagnostic
+  // will have been deferred.
+  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed() &&
+      D->getType()->isDependentType())
     SemaRef.DiagnoseUnusedDecl(Var);
 
   return Var;
index 7e26c786ed1578d15dceb258c6539eae0ebb2bb8..7b7d84d3f9c870d06252ff0621886b5065e470aa 100644 (file)
@@ -5,8 +5,7 @@
 template <typename T>
   struct Vector {
     void doIt() {
-      // FIXME: PR10168: Only warn once for this!
-      int a; // expected-warning 2{{unused variable 'a'}}
+      int a; // expected-warning {{unused variable 'a'}}
 
       for (auto& e : elements)
         ;
@@ -18,5 +17,5 @@ template <typename T>
 
 int main(int, char**) {
   Vector<int>    vector;
-  vector.doIt(); // expected-note {{requested here}}
+  vector.doIt();
 }
index 81f22a796a0e64c6a8daf27df8766934f0be3602..5ba1f2a5f34b21b24ad7304c5b14cbb0504b6824 100644 (file)
@@ -54,3 +54,29 @@ void unused_local_static() {
   static int y = 0; // expected-warning{{unused variable 'y'}}
 #pragma unused(x)
 }
+
+// PR10168
+namespace PR10168 {
+  // We expect a warning in the definition only for non-dependent variables, and
+  // a warning in the instantiation only for dependent variables.
+  template<typename T>
+  struct S {
+    void f() {
+      int a; // expected-warning {{unused variable 'a'}}
+      T b; // expected-warning 2{{unused variable 'b'}}
+    }
+  };
+
+  template<typename T>
+  void f() {
+    int a; // expected-warning {{unused variable 'a'}}
+    T b; // expected-warning 2{{unused variable 'b'}}
+  }
+
+  void g() {
+    S<int>().f(); // expected-note {{here}}
+    S<char>().f(); // expected-note {{here}}
+    f<int>(); // expected-note {{here}}
+    f<char>(); // expected-note {{here}}
+  }
+}