[clang-tools-extra] [clang-tidy] Add detection of decltype(nullptr) => std::nullptr_t to … (PR #224591)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 18 03:09:35 PDT 2026
https://github.com/serge-sans-paille created https://github.com/llvm/llvm-project/pull/224591
…modernize-use-nullptr
Hidden behind an option switch, but on by default.
>From bcc52f1caef544ac8cbd301a32dd6a998f9b7740 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Fri, 18 Sep 2026 11:54:00 +0200
Subject: [PATCH] [clang-tidy] Add detection of decltype(nullptr) =>
std::nullptr_t to modernize-use-nullptr
Hidden behind an option switch, but on by default.
---
.../clang-tidy/modernize/UseNullptrCheck.cpp | 36 ++++++++++++++++++-
.../clang-tidy/modernize/UseNullptrCheck.h | 5 +++
clang-tools-extra/docs/ReleaseNotes.md | 4 +++
.../checks/modernize/use-nullptr.rst | 15 ++++++++
.../checkers/modernize/use-nullptr-basic.cpp | 14 ++++++++
.../checkers/modernize/use-nullptr.cpp | 2 +-
6 files changed, 74 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
index 4f561a1f10204..4b26af5836ba2 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
@@ -28,6 +28,12 @@ AST_MATCHER(Type, sugaredNullptrType) {
return false;
}
+AST_MATCHER(DecltypeType, decltypeTypeNullptrLiteral) {
+ if (const Expr *E = Node.getUnderlyingExpr())
+ return isa<CXXNullPtrLiteralExpr>(E);
+ return false;
+}
+
} // namespace
static constexpr char CastSequence[] = "sequence";
@@ -81,6 +87,11 @@ void UseNullptrCheck::registerMatchers(MatchFinder *Finder) {
// Skip defaulted comparison operators.
unless(hasAncestor(functionDecl(isDefaulted())))),
this);
+
+ if (NullptrCStddef)
+ Finder->addMatcher(typeLoc(loc(decltypeType(decltypeTypeNullptrLiteral())))
+ .bind("matchDecltypeNullptr"),
+ this);
}
static bool isReplaceableRange(SourceLocation StartLoc, SourceLocation EndLoc,
@@ -495,17 +506,40 @@ UseNullptrCheck::UseNullptrCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
NullMacrosStr(Options.get("NullMacros", "NULL")),
IgnoredTypes(utils::options::parseStringList(Options.get(
- "IgnoredTypes", "_CmpUnspecifiedParam;^std::__cmp_cat::__unspec"))) {
+ "IgnoredTypes", "_CmpUnspecifiedParam;^std::__cmp_cat::__unspec"))),
+ NullptrCStddef(Options.get("NullptrCStddef", true)),
+ IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::IS_LLVM),
+ areDiagsSelfContained()) {
NullMacrosStr.split(NullMacros, ",");
}
+void UseNullptrCheck::registerPPCallbacks(const SourceManager &SM,
+ Preprocessor *PP,
+ Preprocessor *ModuleExpanderPP) {
+ IncludeInserter.registerPreprocessor(PP);
+}
+
void UseNullptrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "NullMacros", NullMacrosStr);
Options.store(Opts, "IgnoredTypes",
utils::options::serializeStringList(IgnoredTypes));
+ Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
+ Options.store(Opts, "NullptrCStddef", NullptrCStddef);
}
void UseNullptrCheck::check(const MatchFinder::MatchResult &Result) {
+ if (const auto *MatchedTypeLoc =
+ Result.Nodes.getNodeAs<TypeLoc>("matchDecltypeNullptr")) {
+ diag(MatchedTypeLoc->getBeginLoc(), "use std::nullptr_t instead")
+ << IncludeInserter.createIncludeInsertion(
+ Result.SourceManager->getFileID(MatchedTypeLoc->getBeginLoc()),
+ "<cstddef>")
+ << FixItHint::CreateReplacement(MatchedTypeLoc->getSourceRange(),
+ "std::nullptr_t");
+ return;
+ }
+
const auto *NullCast = Result.Nodes.getNodeAs<CastExpr>(CastSequence);
assert(NullCast && "Bad Callback. No node provided");
diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.h b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.h
index 1caa07afe352a..18df06a346bd6 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.h
@@ -10,6 +10,7 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USENULLPTRCHECK_H
#include "../ClangTidyCheck.h"
+#include "../utils/IncludeInserter.h"
namespace clang::tidy::modernize {
@@ -19,6 +20,8 @@ class UseNullptrCheck : public ClangTidyCheck {
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus11 || LangOpts.C23;
}
+ void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
+ Preprocessor *ModuleExpanderPP) override;
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
@@ -27,6 +30,8 @@ class UseNullptrCheck : public ClangTidyCheck {
const StringRef NullMacrosStr;
SmallVector<StringRef, 1> NullMacros;
std::vector<StringRef> IgnoredTypes;
+ const bool NullptrCStddef;
+ utils::IncludeInserter IncludeInserter;
};
} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index b77d0b5f5b7ec..a910c9d656ba5 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -224,6 +224,10 @@ infrastructure are described first, followed by tool-specific sections.
<clang-tidy/checks/modernize/use-noexcept>` when analyzing malformed template
code with an unparsed exception specification.
+- Extend {doc}`modernize-use-nullptr
+ <clang-tidy/checks/modernize/use-nullptr>` to turn ``decltype(nullptr)`` into
+ ``std::nullptr_t`` from ``<cstdef>``.
+
- Improved {doc}`performance-inefficient-algorithm
<clang-tidy/checks/performance/inefficient-algorithm>` check to no longer
produce a fix with the container or the searched-for value missing, such as
diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-nullptr.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-nullptr.rst
index 25e17fee0a3d6..4ad115b4e7e3b 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-nullptr.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-nullptr.rst
@@ -6,6 +6,9 @@ modernize-use-nullptr
The check converts the usage of null pointer constants (e.g. ``NULL``, ``0``)
to use the new C++11 and C23 ``nullptr`` keyword.
+It also replaces references to ``decltype(nullptr)`` with ``std::nullptr_t``
+from ``<cstdef>``.
+
Example
-------
@@ -21,11 +24,15 @@ Example
return 0;
}
+ void expect_null(decltype(nullptr));
+
transforms to:
.. code-block:: c++
+ #include <cstddef>
+
void assignment() {
char *a = nullptr;
char *b = nullptr;
@@ -36,6 +43,9 @@ transforms to:
return nullptr;
}
+ void expect_null(std::nullptr_t);
+
+
Options
-------
@@ -51,6 +61,11 @@ Options
``NULL``. By default this check will only replace the ``NULL`` macro and will
skip any similar user-defined macros.
+.. option:: NullptrCStddef
+
+ Boolean controlling wether we should replace ``decltype(nullptr)`` with the
+ type ``std::nullptr_t`` from ``cstddef``. Defaults to ``true``.
+
Example
^^^^^^^
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-basic.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-basic.cpp
index 7b92cbd9b7608..621ddfe3f60ec 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-basic.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr-basic.cpp
@@ -292,3 +292,17 @@ template<typename T>
T *f2(T *a = NULL) {
return a ? a : NULL;
}
+
+void foo(decltype(nullptr));
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::nullptr_t instead
+// CHECK-FIXES: void foo(std::nullptr_t);
+void foo(const decltype(nullptr));
+// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::nullptr_t instead
+// CHECK-FIXES: void foo(const std::nullptr_t);
+decltype(nullptr) a;
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use std::nullptr_t instead
+// CHECK-FIXES: std::nullptr_t a;
+template<class T=decltype(nullptr)>
+struct bar {};
+// CHECK-MESSAGES: :[[@LINE-2]]:18: warning: use std::nullptr_t instead
+// CHECK-FIXES: template<class T=std::nullptr_t>
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp
index 0092a5cc9a47b..3d4b951ba9336 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp
@@ -1,5 +1,5 @@
// RUN: %check_clang_tidy %s modernize-use-nullptr %t -- \
-// RUN: -config="{CheckOptions: {modernize-use-nullptr.NullMacros: 'MY_NULL,NULL'}}"
+// RUN: -config="{CheckOptions: {modernize-use-nullptr.NullMacros: 'MY_NULL,NULL', modernize-use-nullptr.NullptrCStddef: false}}"
#include <cstddef>
More information about the cfe-commits
mailing list