[clang-tools-extra] 90a9552 - [clang-tidy] Ignore 0 and NULL null pointers in modernize-use-emplace (#237770)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 17:46:06 PST 2026
Author: Andrew Gaul
Date: 2026-11-05T09:46:01+08:00
New Revision: 90a95524c33efe7f59c99eedbc6f9710c7a59ab8
URL: https://github.com/llvm/llvm-project/commit/90a95524c33efe7f59c99eedbc6f9710c7a59ab8
DIFF: https://github.com/llvm/llvm-project/commit/90a95524c33efe7f59c99eedbc6f9710c7a59ab8.diff
LOG: [clang-tidy] Ignore 0 and NULL null pointers in modernize-use-emplace (#237770)
The check rewrites
```
v.push_back(T(42, 0));
```
where the second parameter of the constructor of `T` is a pointer, to
`v.emplace_back(42, 0)`. The forwarding reference of `emplace_back`
deduces `0` as an `int`, which, unlike the literal, does not convert to
a pointer, so the rewritten call does not compile. `NULL`, which is
`__null` or an integer literal, has the same problem, as does a literal
zero passed for a pointer to member.
Ignore such calls, as the check already does for bit-field arguments,
and remove the FIXME about them. `nullptr` is deduced as
`std::nullptr_t`, which converts to any pointer, so calls passing it are
still reported.
Assisted-by: Claude Opus 5.5
---------
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
Added:
Modified:
clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp
clang-tools-extra/docs/ReleaseNotes.md
clang-tools-extra/docs/clang-tidy/checks/modernize/use-emplace.rst
clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp
index 8ec123fc476ff4..ea033e0794a995 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp
@@ -181,6 +181,13 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) {
ignoringImplicit(allOf(cxxConstructExpr(isListInitialization()),
unless(cxxTemporaryObjectExpr()))));
+ // 0 and NULL convert to null pointers, but emplace_back would deduce them as
+ // integers, which don't.
+ auto NullPointerConstantAsArgument = hasAnyArgument(
+ expr(hasType(hasCanonicalType(anyOf(pointerType(), memberPointerType()))),
+ ignoringImplicit(
+ ignoringParenImpCasts(anyOf(integerLiteral(), gnuNullExpr())))));
+
// We could have leak of resource.
auto NewExprAsArgument = hasAnyArgument(ignoringImplicit(cxxNewExpr()));
// We would call another constructor.
@@ -194,13 +201,14 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) {
auto HasInitList = anyOf(has(ignoringImplicit(initListExpr())),
has(cxxStdInitializerListExpr()));
- // FIXME: Discard 0/NULL (as nullptr), static inline const data members,
- // overloaded functions and template names.
+ // FIXME: Discard static inline const data members, overloaded functions and
+ // template names.
const auto SoughtConstructExpr =
cxxConstructExpr(
unless(anyOf(IsCtorOfSmartPtr, HasInitList, BitFieldAsArgument,
- InitializerListAsArgument, NewExprAsArgument,
- ConstructingDerived, IsPrivateOrProtectedCtor)))
+ NullPointerConstantAsArgument, InitializerListAsArgument,
+ NewExprAsArgument, ConstructingDerived,
+ IsPrivateOrProtectedCtor)))
.bind("ctor");
auto HasConstructExpr = has(ignoringImplicit(SoughtConstructExpr));
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 63e036e123cb8c..6bd09286ec60fa 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -306,6 +306,11 @@ infrastructure are described first, followed by tool-specific sections.
<clang-tidy/checks/modernize/use-designated-initializers>` when analyzing
malformed code with nested classes and ambiguous initializer.
+- Fixed false positive in {doc}`modernize-use-emplace
+ <clang-tidy/checks/modernize/use-emplace>` check when a constructor argument
+ is `0` or `NULL` used as a null pointer, which an `emplace`-like method would
+ forward as an integer.
+
- Improved {doc}`modernize-use-equals-default
<clang-tidy/checks/modernize/use-equals-default>` check to also capture
default constructor with explicit default-initialization of parents.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-emplace.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-emplace.rst
index f1031f69a94679..bdcd6126298a83 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-emplace.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-emplace.rst
@@ -111,6 +111,9 @@ Check also doesn't fire if any argument of the constructor call would be:
- a bit-field (bit-fields can't bind to rvalue/universal reference)
+ - ``0`` or ``NULL`` converted to a null pointer (a universal reference
+ deduces them as integers, which don't convert to pointers)
+
- a ``new`` expression (to avoid leak)
- if the argument would be converted via derived-to-base cast.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp
index a2ab19efcf6981..d111d687dfb718 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp
@@ -196,6 +196,92 @@ void testNewExpr() {
v.push_back(Derived(new int, Something{}));
}
+struct WithMember {
+ int Member;
+ void method();
+};
+
+struct MemberPointer {
+ MemberPointer(int WithMember::*) {}
+};
+
+struct MemberFunctionPointer {
+ MemberFunctionPointer(void (WithMember::*)()) {}
+};
+
+struct FunctionPointer {
+ FunctionPointer(void (*)()) {}
+};
+
+using IntPointer = int *;
+
+struct PointerAlias {
+ PointerAlias(IntPointer) {}
+};
+
+struct PointerReference {
+ PointerReference(int *const &) {}
+};
+
+#define NULL_CONSTANT 0
+
+void testNullPointerConstants() {
+ std::vector<Base> v;
+ v.push_back(Base(42, 0));
+ v.push_back(Base(42, (0)));
+ v.push_back(Base(42, 0L));
+ v.push_back(Base(42, __null));
+ v.push_back(Base(42, NULL_CONSTANT));
+ v.emplace_back(Base(42, 0));
+
+ v.push_back(Base(42, nullptr));
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back
+ // CHECK-FIXES: v.emplace_back(42, nullptr);
+
+ v.push_back(Base(0, nullptr, 0));
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back
+ // CHECK-FIXES: v.emplace_back(0, nullptr, 0);
+
+ v.push_back(Base(42, static_cast<int *>(0)));
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back
+ // CHECK-FIXES: v.emplace_back(42, static_cast<int *>(0));
+
+ v.push_back(Base(42, (int *)0));
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back
+ // CHECK-FIXES: v.emplace_back(42, (int *)0);
+
+ v.emplace_back(Base(42, nullptr));
+ // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: unnecessary temporary object created while calling emplace_back
+ // CHECK-FIXES: v.emplace_back(42, nullptr);
+
+ std::vector<MemberPointer> w;
+ w.push_back(MemberPointer(0));
+
+ w.push_back(MemberPointer(nullptr));
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back
+ // CHECK-FIXES: w.emplace_back(nullptr);
+
+ std::vector<MemberFunctionPointer> x;
+ x.push_back(MemberFunctionPointer(0));
+
+ x.push_back(MemberFunctionPointer(nullptr));
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back
+ // CHECK-FIXES: x.emplace_back(nullptr);
+
+ std::vector<FunctionPointer> y;
+ y.push_back(FunctionPointer(0));
+
+ y.push_back(FunctionPointer(nullptr));
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back
+ // CHECK-FIXES: y.emplace_back(nullptr);
+
+ std::vector<PointerAlias> z;
+ z.push_back(PointerAlias(0));
+
+ std::vector<PointerReference> r;
+ r.push_back(PointerReference(0));
+}
+
void testSpaces() {
std::vector<Something> v;
More information about the cfe-commits
mailing list