[PATCH] D116488: Add a misc-unused-parameters.CommentOutUnusedParameters to clang-tidy

Mehdi AMINI via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 1 16:59:36 PST 2022


mehdi_amini created this revision.
mehdi_amini added reviewers: rriddle, jpienaar, Mogball.
Herald added a subscriber: carlosgalvezp.
mehdi_amini requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

This option allows to eliminate parameter names instead of commenting
them out.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116488

Files:
  clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
  clang-tools-extra/docs/clang-tidy/checks/misc-unused-parameters.rst
  clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters-no-comment.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters-no-comment.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters-no-comment.cpp
@@ -0,0 +1,18 @@
+// RUN: %check_clang_tidy %s misc-unused-parameters %t -- \
+// RUN:   -config="{CheckOptions: [{key: misc-unused-parameters.CommentOutUnusedParameters, value: false}]}" --
+
+// Check that we eliminate parameters instead of commenting them out.
+class SomeClass {
+  static void f(int i) { ; }
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning
+  // CHECK-FIXES: static void f(int ) {;}
+  static void g(int i = 1) { ; }
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning
+  // CHECK-FIXES: static void g(int = 1) {;}
+  static void h(int i[]) { ; }
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning
+  // CHECK-FIXES: static void h(int []) {;}
+  static void s(void (*fn)()) { ; }
+  // CHECK-MESSAGES: :[[@LINE-1]]:24: warning
+  // CHECK-FIXES: static void s(void (*)()) {;}
+};
Index: clang-tools-extra/docs/clang-tidy/checks/misc-unused-parameters.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/misc-unused-parameters.rst
+++ clang-tools-extra/docs/clang-tidy/checks/misc-unused-parameters.rst
@@ -20,6 +20,10 @@
 
   void a(int  /*i*/) { /*some code that doesn't use `i`*/ }
 
+  // or if the CommentOutUnusedParameters is set to false:
+
+  void a(int ) { /*some code that doesn't use `i`*/ }
+
 .. code-block:: c++
 
   static void staticFunctionA(int i);
@@ -40,3 +44,8 @@
    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:: misc-unused-parameters.CommentOutUnusedParameters
+
+   When `true` (default value), the suggest fix will comment out the parameter name
+   instead of removing them.
Index: clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
+++ clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
@@ -154,8 +154,11 @@
     // Note: We always add a space before the '/*' to not accidentally create
     // a '*/*' for pointer types, which doesn't start a comment. clang-format
     // will clean this up afterwards.
-    MyDiag << FixItHint::CreateReplacement(
-        RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
+    if (Options.get("CommentOutUnusedParameters", true))
+      MyDiag << FixItHint::CreateReplacement(
+          RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
+    else
+      MyDiag << FixItHint::CreateReplacement(RemovalRange, "");
     return;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116488.396888.patch
Type: text/x-patch
Size: 2877 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220102/58b94c21/attachment.bin>


More information about the cfe-commits mailing list