diff --git a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp --- a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp +++ b/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; } 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 --- a/clang-tools-extra/docs/clang-tidy/checks/misc-unused-parameters.rst +++ b/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. diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters-no-comment.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc-unused-parameters-no-comment.cpp new file mode 100644 --- /dev/null +++ b/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 (*)()) {;} +};