[clang-tools-extra] 2447939 - [Clang-Tidy] Skip `misc-unused-parameters` in macro. (#194999)
via cfe-commits
cfe-commits at lists.llvm.org
Sat May 2 03:31:41 PDT 2026
Author: Dmitrii Kuragin
Date: 2026-05-02T18:31:36+08:00
New Revision: 244793958719d8787e6c6e66abf40a9abc6c3227
URL: https://github.com/llvm/llvm-project/commit/244793958719d8787e6c6e66abf40a9abc6c3227
DIFF: https://github.com/llvm/llvm-project/commit/244793958719d8787e6c6e66abf40a9abc6c3227.diff
LOG: [Clang-Tidy] Skip `misc-unused-parameters` in macro. (#194999)
The new parameter allows to skip the check for the cases when we need to
use the macro, but there is no immediate way to fix the macro itself. It
recently come up with a gradual adoption of clang-tidy and not all parts
of the code could be fixed at once.
Simply enabling it using `clang-tidy-diff` is not enough, since
`misc-unused-parameters` would cause false-positive, since the given
diff didn't introduce the unused parameters and might be not easy to
change.
The given parameters allow for better incremental adoption.
---------
Co-authored-by: Dmitrii Kuragin <dkuragin at adobe.com>
Co-authored-by: EugeneZelenko <eugene.zelenko at gmail.com>
Added:
clang-tools-extra/test/clang-tidy/checkers/misc/unused-parameters-macro.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
clang-tools-extra/test/clang-tidy/infrastructure/dump-config-filtering.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
index 6cebb35a9dad7..b95c829258b67 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.cpp
@@ -130,11 +130,13 @@ UnusedParametersCheck::UnusedParametersCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
StrictMode(Options.get("StrictMode", false)),
- IgnoreVirtual(Options.get("IgnoreVirtual", false)) {}
+ IgnoreVirtual(Options.get("IgnoreVirtual", false)),
+ IgnoreMacroParameters(Options.get("IgnoreMacroParameters", false)) {}
void UnusedParametersCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "StrictMode", StrictMode);
Options.store(Opts, "IgnoreVirtual", IgnoreVirtual);
+ Options.store(Opts, "IgnoreMacroParameters", IgnoreMacroParameters);
}
void UnusedParametersCheck::warnOnUnusedParameter(
@@ -198,6 +200,8 @@ void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
// type if we remove the preceding parameter name.
continue;
}
+ if (IgnoreMacroParameters && Param->getLocation().isMacroID())
+ continue;
// In non-strict mode ignore function definitions with empty bodies
// (constructor initializer counts for non-empty body).
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.h b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.h
index fe2cc6e46c34e..45bae26db633c 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/UnusedParametersCheck.h
@@ -26,6 +26,7 @@ class UnusedParametersCheck : public ClangTidyCheck {
private:
const bool StrictMode;
const bool IgnoreVirtual;
+ const bool IgnoreMacroParameters;
class IndexerVisitor;
std::unique_ptr<IndexerVisitor> Indexer;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 2419917778182..c9749df481bcd 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -393,6 +393,11 @@ Changes in existing checks
- Fixed the `CheckThrowTemporaries` option to correctly reflect its
configured value in exported settings.
+- Improved :doc:`misc-unused-parameters
+ <clang-tidy/checks/misc/unused-parameters>` check by adding
+ `IgnoreMacroParameters` option to suppress warnings for unused parameters
+ whose declarations originate from macro expansions.
+
- Improved :doc:`misc-unused-using-decls
<clang-tidy/checks/misc/unused-using-decls>` to not diagnose ``using``
declarations as unused if they're exported from a module.
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 3d902452f81eb..1d5a3753647de 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
@@ -45,3 +45,11 @@ Options
Determines whether virtual method parameters should be inspected.
Set to `true` to ignore them. Default is `false`.
+
+.. option:: IgnoreMacroParameters
+
+ When `true`, the check will not report unused parameters whose declarations
+ originate from a macro expansion. This suppresses false positives in code
+ that uses macros to define function signatures where parameters are
+ structurally required by the macro but not used in every expansion.
+ Default is `false`.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/unused-parameters-macro.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/unused-parameters-macro.cpp
new file mode 100644
index 0000000000000..231653e61ad23
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/unused-parameters-macro.cpp
@@ -0,0 +1,21 @@
+// RUN: %check_clang_tidy %s misc-unused-parameters %t -- \
+// RUN: -config="{CheckOptions: {misc-unused-parameters.IgnoreMacroParameters: true, \
+// RUN: misc-unused-parameters.StrictMode: true}}" --
+
+// Parameters declared inside a macro expansion should not be reported when
+// IgnoreMacroParameters is enabled.
+
+#define HANDLER(RetType, ParamType, ParamName) \
+ RetType handler_##ParamName(ParamType ParamName) { return 0; }
+
+HANDLER(int, int, unused_from_macro)
+// No warning: parameter comes from macro expansion.
+
+#define WRAP_PARAM(type) type macro_arg
+void functionWithMacroParam(WRAP_PARAM(int)) {}
+// No warning: parameter name comes from macro expansion.
+
+// Regular unused parameter should still be reported.
+void normalFunction(int unused_normal) {}
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: parameter 'unused_normal' is unused [misc-unused-parameters]
+// CHECK-FIXES: void normalFunction(int /*unused_normal*/) {}
diff --git a/clang-tools-extra/test/clang-tidy/infrastructure/dump-config-filtering.cpp b/clang-tools-extra/test/clang-tidy/infrastructure/dump-config-filtering.cpp
index f22ec7edb1775..5fc95ae319803 100644
--- a/clang-tools-extra/test/clang-tidy/infrastructure/dump-config-filtering.cpp
+++ b/clang-tools-extra/test/clang-tidy/infrastructure/dump-config-filtering.cpp
@@ -2,6 +2,7 @@
// RUN: clang-tidy -checks='-*' -dump-config %s -- 2>/dev/null | FileCheck %s --check-prefix=CHECK-DISABLED
// CHECK: CheckOptions:
+// CHECK-NEXT: misc-unused-parameters.IgnoreMacroParameters: 'false'
// CHECK-NEXT: misc-unused-parameters.IgnoreVirtual: 'false'
// CHECK-NEXT: misc-unused-parameters.StrictMode: 'false'
// CHECK-NEXT: SystemHeaders: false
More information about the cfe-commits
mailing list