[clang-tools-extra] r243267 - misc-unused-parameters: Don't warn on ParmVarDecls in the return type.

Daniel Jasper djasper at google.com
Mon Jul 27 06:46:37 PDT 2015


Author: djasper
Date: Mon Jul 27 08:46:37 2015
New Revision: 243267

URL: http://llvm.org/viewvc/llvm-project?rev=243267&view=rev
Log:
misc-unused-parameters: Don't warn on ParmVarDecls in the return type.

As there don't seem to be a good way of formulating a matcher that
finds all pairs of functions and their ParmVarDecls, just match on
functionDecls and iterate over their parameters. This should also be
more efficient as some checks are only performed once per function.

Modified:
    clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp
    clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.h
    clang-tools-extra/trunk/test/clang-tidy/misc-unused-parameters.cpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp?rev=243267&r1=243266&r2=243267&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp Mon Jul 27 08:46:37 2015
@@ -17,9 +17,7 @@ namespace clang {
 namespace tidy {
 
 void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
-  Finder->addMatcher(
-      parmVarDecl(hasParent(functionDecl().bind("function"))).bind("x"),
-      this);
+  Finder->addMatcher(functionDecl().bind("function"), this);
 }
 
 static FixItHint removeParameter(const FunctionDecl *Function, unsigned Index) {
@@ -54,15 +52,10 @@ static FixItHint removeArgument(const Ca
   return FixItHint::CreateRemoval(RemovalRange);
 }
 
-void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
-  const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
-  if (!Function->doesThisDeclarationHaveABody())
-    return;
-  const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("x");
-  if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
-      Param->hasAttr<UnusedAttr>())
-    return;
-
+void UnusedParametersCheck::warnOnUnusedParameter(
+    const MatchFinder::MatchResult &Result, const FunctionDecl *Function,
+    unsigned ParamIndex) {
+  const auto *Param = Function->getParamDecl(ParamIndex);
   auto MyDiag = diag(Param->getLocation(), "parameter '%0' is unused")
                 << Param->getName();
 
@@ -86,10 +79,6 @@ void UnusedParametersCheck::check(const
     return;
   }
 
-  // Handle local functions by deleting the parameters.
-  unsigned ParamIndex = Param->getFunctionScopeIndex();
-  assert(ParamIndex < Function->getNumParams());
-
   // Fix all redeclarations.
   for (const FunctionDecl *FD : Function->redecls())
     MyDiag << removeParameter(FD, ParamIndex);
@@ -103,5 +92,18 @@ void UnusedParametersCheck::check(const
     MyDiag << removeArgument(Match.getNodeAs<CallExpr>("x"), ParamIndex);
 }
 
+void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
+  if (!Function->doesThisDeclarationHaveABody())
+    return;
+  for (unsigned i = 0, e = Function->getNumParams(); i != e; ++i) {
+    const auto *Param = Function->getParamDecl(i);
+    if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
+        Param->hasAttr<UnusedAttr>())
+      continue;
+    warnOnUnusedParameter(Result, Function, i);
+  }
+}
+
 } // namespace tidy
 } // namespace clang

Modified: clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.h?rev=243267&r1=243266&r2=243267&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.h Mon Jul 27 08:46:37 2015
@@ -23,6 +23,11 @@ public:
       : ClangTidyCheck(Name, Context) {}
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+
+private:
+  void
+  warnOnUnusedParameter(const ast_matchers::MatchFinder::MatchResult &Result,
+                        const FunctionDecl *Function, unsigned ParamIndex);
 };
 
 } // namespace tidy

Modified: clang-tools-extra/trunk/test/clang-tidy/misc-unused-parameters.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-unused-parameters.cpp?rev=243267&r1=243266&r2=243267&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-unused-parameters.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-unused-parameters.cpp Mon Jul 27 08:46:37 2015
@@ -101,3 +101,8 @@ template <typename T> void someFunctionT
 // CHECK-FIXES: {{^}}template <typename T> void someFunctionTemplateAllUnusedParams(T  /*b*/, T  /*e*/) {}
 
 static void dontGetConfusedByParametersInFunctionTypes() { void (*F)(int i); }
+
+template <typename T> class Function {};
+static Function<void(int, int i)> dontGetConfusedByFunctionReturnTypes() {
+  return Function<void(int, int)>();
+}





More information about the cfe-commits mailing list