misc-unused-parameters: Fix handling of parameters in template functions.
authorDaniel Jasper <djasper@google.com>
Wed, 22 Jul 2015 17:30:35 +0000 (17:30 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 22 Jul 2015 17:30:35 +0000 (17:30 +0000)
The parameters of the function templates were being marked as
incorrectly be marked as unused. Added a test for this and changed the
check to use the same

  isReferenced() || !getDeclName()

logic as Sema::DiagnoseUnusedParameters.
Patch Scott Wallace, thank you!

llvm-svn: 242912

clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
clang-tools-extra/test/clang-tidy/misc-unused-parameters.cpp

index bfc213e..8449eaf 100644 (file)
@@ -59,7 +59,8 @@ void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
   if (!Function->doesThisDeclarationHaveABody())
     return;
   const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("x");
-  if (Param->isUsed())
+  if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
+     Param->hasAttr<UnusedAttr>())
     return;
 
   auto MyDiag = diag(Param->getLocation(), "parameter '%0' is unused")
@@ -102,4 +103,3 @@ void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
 
 } // namespace tidy
 } // namespace clang
-
index 3b54cc5..fc8a4ee 100644 (file)
@@ -88,3 +88,14 @@ void someMoreCallSites() {
 }
 
 } // end namespace
+
+template <typename T> void someFunctionTemplate(T b, T e) { (void)b; (void)e; }
+
+template <typename T> void someFunctionTemplateOneUnusedParam(T b, T e) { (void)e; }
+// CHECK-MESSAGES: :[[@LINE-1]]:65: warning
+// CHECK-FIXES: {{^}}template <typename T> void someFunctionTemplateOneUnusedParam(T  /*b*/, T e) { (void)e; }
+
+template <typename T> void someFunctionTemplateAllUnusedParams(T b, T e) {}
+// CHECK-MESSAGES: :[[@LINE-1]]:66: warning
+// CHECK-MESSAGES: :[[@LINE-2]]:71: warning
+// CHECK-FIXES: {{^}}template <typename T> void someFunctionTemplateAllUnusedParams(T  /*b*/, T  /*e*/) {}