https://github.com/xxxxbc updated https://github.com/llvm/llvm-project/pull/208730
>From ff416451658a50a8ad5483f474a03244ebd42fb8 Mon Sep 17 00:00:00 2001 From: hehuan <[email protected]> Date: Fri, 10 Jul 2026 21:10:09 +0800 Subject: [PATCH] [clang-tidy] Ignore standard tag dispatch types in readability-named-parameter The readability-named-parameter check warned on unnamed parameters whose types are standard tag dispatch types. These types are used exclusively for overload resolution, so requiring a name for them only adds noise. Add the TagDispatchTypes option with a comprehensive default list of standard tag dispatch types (std::in_place_t, std::allocator_arg_t, std::nothrow_t, std::piecewise_construct_t, std::defer_lock_t, std::try_to_lock_t, std::default_sentinel_t, std::from_range_t, std::sorted_unique_t, std::sorted_equivalent_t, std::nostopstate_t, std::unexpect_t). Users can customize the list to add their own tag types. Fixes #208448. --- .../readability/NamedParameterCheck.cpp | 32 ++++++++++++++++- .../readability/NamedParameterCheck.h | 1 + clang-tools-extra/docs/ReleaseNotes.rst | 7 ++++ .../checks/readability/named-parameter.rst | 26 ++++++++++++++ .../checkers/readability/named-parameter.cpp | 35 +++++++++++++++++++ 5 files changed, 100 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/readability/NamedParameterCheck.cpp b/clang-tools-extra/clang-tidy/readability/NamedParameterCheck.cpp index c457d682a1787..20de428ee923f 100644 --- a/clang-tools-extra/clang-tidy/readability/NamedParameterCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/NamedParameterCheck.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "NamedParameterCheck.h" +#include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" @@ -15,15 +16,35 @@ using namespace clang::ast_matchers; namespace clang::tidy::readability { +// Standard tag dispatch types that are used exclusively for overload +// resolution and therefore do not need a parameter name. +static constexpr StringRef DefaultTagDispatchTypes = + "std::allocator_arg_t;" + "std::defer_lock_t;" + "std::default_sentinel_t;" + "std::from_range_t;" + "std::in_place_t;" + "std::nothrow_t;" + "std::nostopstate_t;" + "std::piecewise_construct_t;" + "std::sorted_equivalent_t;" + "std::sorted_unique_t;" + "std::try_to_lock_t;" + "std::unexpect_t"; + NamedParameterCheck::NamedParameterCheck(StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), InsertPlainNamesInForwardDecls( - Options.get("InsertPlainNamesInForwardDecls", false)) {} + Options.get("InsertPlainNamesInForwardDecls", false)), + TagDispatchTypes(utils::options::parseStringList( + Options.get("TagDispatchTypes", DefaultTagDispatchTypes))) {} void NamedParameterCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "InsertPlainNamesInForwardDecls", InsertPlainNamesInForwardDecls); + Options.store(Opts, "TagDispatchTypes", + utils::options::serializeStringList(TagDispatchTypes)); } void NamedParameterCheck::registerMatchers(ast_matchers::MatchFinder *Finder) { @@ -75,6 +96,15 @@ void NamedParameterCheck::check(const MatchFinder::MatchResult &Result) { if (Parm->getType().getCanonicalType()->isNullPtrType()) continue; + // Skip standard tag dispatch types used exclusively for overload + // resolution. + if (const auto *Record = + Parm->getType().getCanonicalType()->getAsCXXRecordDecl()) { + const std::string QName = Record->getQualifiedNameAsString(); + if (llvm::is_contained(TagDispatchTypes, QName)) + continue; + } + // Look for comments. We explicitly want to allow idioms like // void foo(int /*unused*/) const char *Begin = SM.getCharacterData(Parm->getBeginLoc()); diff --git a/clang-tools-extra/clang-tidy/readability/NamedParameterCheck.h b/clang-tools-extra/clang-tidy/readability/NamedParameterCheck.h index ecd128d887f84..aa10b1e5ff99b 100644 --- a/clang-tools-extra/clang-tidy/readability/NamedParameterCheck.h +++ b/clang-tools-extra/clang-tidy/readability/NamedParameterCheck.h @@ -36,6 +36,7 @@ class NamedParameterCheck : public ClangTidyCheck { private: const bool InsertPlainNamesInForwardDecls; + const std::vector<StringRef> TagDispatchTypes; }; } // namespace clang::tidy::readability diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 082cc5cb78bc0..2086b91e11191 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -854,6 +854,13 @@ Changes in existing checks <clang-tidy/checks/readability/inconsistent-ifelse-braces>` check to correctly handle labeled statements of ``if``/``else`` bodies. +- Improved :doc:`readability-named-parameter + <clang-tidy/checks/readability/named-parameter>` check by ignoring + standard tag dispatch types (e.g. ``std::in_place_t``, + ``std::allocator_arg_t``, ``std::nothrow_t``) that are used exclusively for + overload resolution. Added the :option:`TagDispatchTypes` option to allow + customizing the set of ignored types. + - Improved :doc:`readability-non-const-parameter <clang-tidy/checks/readability/non-const-parameter>` check: diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/named-parameter.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/named-parameter.rst index 0dd25e23fc653..fc4767c745511 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/readability/named-parameter.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/readability/named-parameter.rst @@ -25,6 +25,11 @@ function definition. Corresponding cpplint.py check name: `readability/function`. +The check ignores parameters whose types are standard tag dispatch types used +exclusively for overload resolution (e.g., ``std::in_place_t``, +``std::allocator_arg_t``, ``std::nothrow_t``). The set of ignored types can be +customized with the :option:`TagDispatchTypes` option. + Options ------- @@ -33,3 +38,24 @@ Options If set to `true`, the check will insert parameter names without comments for forward declarations only. Otherwise, the check will insert parameter names as comments (e.g., ``/*param*/``). Default is `false`. + +.. option:: TagDispatchTypes + + A semicolon-separated list of fully-qualified type names that should be + treated as tag dispatch types. Parameters of these types are not required to + be named. Defaults to the standard tag dispatch types: + + .. code-block:: text + + std::allocator_arg_t + std::defer_lock_t + std::default_sentinel_t + std::from_range_t + std::in_place_t + std::nothrow_t + std::nostopstate_t + std::piecewise_construct_t + std::sorted_equivalent_t + std::sorted_unique_t + std::try_to_lock_t + std::unexpect_t diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/named-parameter.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/named-parameter.cpp index 8ae0d7055867b..1db5975f2b7a5 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/named-parameter.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/named-parameter.cpp @@ -161,10 +161,45 @@ void MockFunction(Unused, int q, Unused) { namespace std { typedef decltype(nullptr) nullptr_t; +struct allocator_arg_t {}; +struct defer_lock_t {}; +struct default_sentinel_t {}; +struct from_range_t {}; +struct in_place_t {}; +struct nothrow_t {}; +struct nostopstate_t {}; +struct piecewise_construct_t {}; +struct sorted_equivalent_t {}; +struct sorted_unique_t {}; +struct try_to_lock_t {}; +struct unexpect_t {}; } void f(std::nullptr_t) {} +// Standard tag dispatch types should not trigger warnings. +void f_in_place(std::in_place_t) {} +void f_allocator_arg(std::allocator_arg_t) {} +void f_default_sentinel(std::default_sentinel_t) {} +void f_nothrow(std::nothrow_t) {} +void f_piecewise(std::piecewise_construct_t) {} +void f_defer_lock(std::defer_lock_t) {} +void f_try_to_lock(std::try_to_lock_t) {} +void f_from_range(std::from_range_t) {} +void f_sorted_unique(std::sorted_unique_t) {} +void f_sorted_equivalent(std::sorted_equivalent_t) {} +void f_nostopstate(std::nostopstate_t) {} +void f_unexpect(std::unexpect_t) {} + +// A non-standard type that is not in the TagDispatchTypes list should still +// trigger a warning. +struct NotATag {}; +void f_not_a_tag(NotATag) {} +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: all parameters should be named in a function +// CHECK-FIXES: void f_not_a_tag(NotATag /*unused*/) {} +// CHECK-MESSAGES-PLAIN-NAMES: :[[@LINE-3]]:25: warning: all parameters should be named in a function +// CHECK-FIXES-PLAIN-NAMES: void f_not_a_tag(NotATag /*unused*/) {} + typedef void (F)(int); F f; void f(int x) {} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
