[clang-tools-extra] [clang-tidy] bugprone-unhandled-self-assignment: fix smart pointer check against std::unique_ptr type (PR #121266)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 30 03:48:51 PST 2024
https://github.com/flovent updated https://github.com/llvm/llvm-project/pull/121266
>From 009178ad073cd76630418e74092907e1d9dc0d85 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sat, 28 Dec 2024 21:52:53 +0800
Subject: [PATCH 1/2] [clang-tidy] bugprone-unhandled-self-assignment: fix
smart pointer check against std::unique_ptr type
---
.../clang-tidy/bugprone/UnhandledSelfAssignmentCheck.cpp | 8 +++++---
.../checkers/bugprone/unhandled-self-assignment.cpp | 4 +++-
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/UnhandledSelfAssignmentCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnhandledSelfAssignmentCheck.cpp
index 8121a36f803460..1f432c4ccc5f00 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnhandledSelfAssignmentCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnhandledSelfAssignmentCheck.cpp
@@ -74,9 +74,11 @@ void UnhandledSelfAssignmentCheck::registerMatchers(MatchFinder *Finder) {
// Matcher for standard smart pointers.
const auto SmartPointerType = qualType(hasUnqualifiedDesugaredType(
recordType(hasDeclaration(classTemplateSpecializationDecl(
- hasAnyName("::std::shared_ptr", "::std::unique_ptr",
- "::std::weak_ptr", "::std::auto_ptr"),
- templateArgumentCountIs(1))))));
+ anyOf(allOf(hasAnyName("::std::shared_ptr", "::std::weak_ptr",
+ "::std::auto_ptr"),
+ templateArgumentCountIs(1)),
+ allOf(hasName("::std::unique_ptr"),
+ templateArgumentCountIs(2))))))));
// We will warn only if the class has a pointer or a C array field which
// probably causes a problem during self-assignment (e.g. first resetting
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unhandled-self-assignment.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unhandled-self-assignment.cpp
index 14d27855d7c5a6..8610393449f97f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unhandled-self-assignment.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unhandled-self-assignment.cpp
@@ -10,7 +10,9 @@ template <class T>
T &&move(T &x) {
}
-template <class T>
+template <typename T> class default_delete {};
+
+template <class T, typename Deleter = std::default_delete<T>>
class unique_ptr {
};
>From 4de37e9b224bdcf51991779ae49146fd553fe99d Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Mon, 30 Dec 2024 19:48:32 +0800
Subject: [PATCH 2/2] add release note for last commit in ReleaseNotes.rst
---
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index fabd0cc78ac645..24b2e61a6f17ee 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -232,6 +232,10 @@ Changes in existing checks
`bsl::optional` and `bdlb::NullableValue` from
<https://github.com/bloomberg/bde>_.
+- Improved :doc:`bugprone-unhandled-self-assignment
+ <clang-tidy/checks/bugprone/unhandled-self-assignment>` check by fixing smart
+ pointer check against std::unique_ptr type.
+
- Improved :doc:`bugprone-unsafe-functions
<clang-tidy/checks/bugprone/unsafe-functions>` check to allow specifying
additional functions to match.
More information about the cfe-commits
mailing list