[clang-tools-extra] 225d255 - [clang-tidy] Added IgnoreVirtual option to misc-unused-parameters

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 10 07:56:08 PDT 2023


Author: Piotr Zegar
Date: 2023-04-10T14:41:32Z
New Revision: 225d255a583ea3d50bbba49d949ca76be6a880bf

URL: https://github.com/llvm/llvm-project/commit/225d255a583ea3d50bbba49d949ca76be6a880bf
DIFF: https://github.com/llvm/llvm-project/commit/225d255a583ea3d50bbba49d949ca76be6a880bf.diff

LOG: [clang-tidy] Added IgnoreVirtual option to misc-unused-parameters

Added option to ignore virtual methods in this check.
This allows to quickly get rid of big number of
potentialy false-positive issues without inserting
not-needed comments.

Fixes #55665.

Reviewed By: njames93

Differential Revision: https://reviews.llvm.org/D147918

Added: 
    clang-tools-extra/test/clang-tidy/checkers/misc/unused-parameters-virtual.cpp

Modified: 
    clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
    clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.h
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/docs/clang-tidy/checks/misc/unused-parameters.rst

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
index 2c69cb0df7137..3f1d2f9f58099 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
@@ -123,10 +123,12 @@ UnusedParametersCheck::~UnusedParametersCheck() = default;
 UnusedParametersCheck::UnusedParametersCheck(StringRef Name,
                                              ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
-      StrictMode(Options.getLocalOrGlobal("StrictMode", false)) {}
+      StrictMode(Options.getLocalOrGlobal("StrictMode", false)),
+      IgnoreVirtual(Options.get("IgnoreVirtual", false)) {}
 
 void UnusedParametersCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "StrictMode", StrictMode);
+  Options.store(Opts, "IgnoreVirtual", IgnoreVirtual);
 }
 
 void UnusedParametersCheck::warnOnUnusedParameter(
@@ -176,9 +178,12 @@ void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
   const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
   if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
     return;
-  if (const auto *Method = dyn_cast<CXXMethodDecl>(Function))
+  if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
+    if (IgnoreVirtual && Method->isVirtual())
+      return;
     if (Method->isLambdaStaticInvoker())
       return;
+  }
   for (unsigned I = 0, E = Function->getNumParams(); I != E; ++I) {
     const auto *Param = Function->getParamDecl(I);
     if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||

diff  --git a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.h b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.h
index 684c95daaae72..90097ed415d37 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.h
@@ -25,6 +25,7 @@ class UnusedParametersCheck : public ClangTidyCheck {
 
 private:
   const bool StrictMode;
+  const bool IgnoreVirtual;
   class IndexerVisitor;
   std::unique_ptr<IndexerVisitor> Indexer;
 

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 64f89dfb6034b..cb10385d8e87f 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -220,6 +220,10 @@ Changes in existing checks
   <clang-tidy/checks/misc/definitions-in-headers>` to avoid warning on
   declarations inside anonymous namespaces.
 
+- Improved :doc:`misc-unused-parameters
+  <clang-tidy/checks/misc/unused-parameters>` check with new `IgnoreVirtual`
+  option to optionally ignore virtual methods.
+
 - Deprecated check-local options `HeaderFileExtensions`
   in :doc:`misc-unused-using-decls
   <clang-tidy/checks/misc/unused-using-decls>` check.

diff  --git a/clang-tools-extra/docs/clang-tidy/checks/misc/unused-parameters.rst b/clang-tools-extra/docs/clang-tidy/checks/misc/unused-parameters.rst
index 7a454a283b7c1..87b75579d97a7 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/misc/unused-parameters.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/misc/unused-parameters.rst
@@ -40,3 +40,8 @@ Options
    constructors - no constructor initializers). When the function body is empty,
    an unused parameter is unlikely to be unnoticed by a human reader, and
    there's basically no place for a bug to hide.
+
+.. option:: IgnoreVirtual
+
+   Determines whether virtual method parameters should be inspected.
+   Set to `true` to ignore them. Default is `false`.

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/misc/unused-parameters-virtual.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/unused-parameters-virtual.cpp
new file mode 100644
index 0000000000000..0ba2965c9b04c
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/unused-parameters-virtual.cpp
@@ -0,0 +1,20 @@
+// RUN: %check_clang_tidy %s misc-unused-parameters %t -- \
+// RUN:   -config="{CheckOptions: [{key: misc-unused-parameters.IgnoreVirtual, value: true}]}" --
+
+struct Base {
+  int f(int foo) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: parameter 'foo' is unused [misc-unused-parameters]
+  // CHECK-FIXES: int f(int  /*foo*/) {
+    return 5;
+  }
+
+  virtual int f2(int foo) {
+    return 5;
+  }
+};
+
+struct Derived : Base {
+  int f2(int foo) override {
+    return 5;
+  }
+};


        


More information about the cfe-commits mailing list