From: Richard Smith Date: Tue, 21 Jun 2011 23:42:09 +0000 (+0000) Subject: Fix PR10168: don't warn for unused non-dependent variables in both the template defin... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e3499cae8e5323ac553ad56977bf1cd42b9a5a35;p=clang Fix PR10168: don't warn for unused non-dependent variables in both the template definition and each instantiation. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@133580 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index e78aa2991e..cc66ec6759 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -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; diff --git a/test/SemaCXX/for-range-unused.cpp b/test/SemaCXX/for-range-unused.cpp index 7e26c786ed..7b7d84d3f9 100644 --- a/test/SemaCXX/for-range-unused.cpp +++ b/test/SemaCXX/for-range-unused.cpp @@ -5,8 +5,7 @@ template 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 int main(int, char**) { Vector vector; - vector.doIt(); // expected-note {{requested here}} + vector.doIt(); } diff --git a/test/SemaCXX/warn-unused-variables.cpp b/test/SemaCXX/warn-unused-variables.cpp index 81f22a796a..5ba1f2a5f3 100644 --- a/test/SemaCXX/warn-unused-variables.cpp +++ b/test/SemaCXX/warn-unused-variables.cpp @@ -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 + struct S { + void f() { + int a; // expected-warning {{unused variable 'a'}} + T b; // expected-warning 2{{unused variable 'b'}} + } + }; + + template + void f() { + int a; // expected-warning {{unused variable 'a'}} + T b; // expected-warning 2{{unused variable 'b'}} + } + + void g() { + S().f(); // expected-note {{here}} + S().f(); // expected-note {{here}} + f(); // expected-note {{here}} + f(); // expected-note {{here}} + } +}