Skip to content

Commit c274837

Browse files
authored
[clang-tidy] bugprone-unhandled-self-assignment: fix smart pointer check against std::unique_ptr type (#121266)
Unlike other standard smart pointer types, std::unique_ptr has two template arguments. testcase need to be updated too.
1 parent 525f526 commit c274837

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

clang-tools-extra/clang-tidy/bugprone/UnhandledSelfAssignmentCheck.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ void UnhandledSelfAssignmentCheck::registerMatchers(MatchFinder *Finder) {
7474
// Matcher for standard smart pointers.
7575
const auto SmartPointerType = qualType(hasUnqualifiedDesugaredType(
7676
recordType(hasDeclaration(classTemplateSpecializationDecl(
77-
hasAnyName("::std::shared_ptr", "::std::unique_ptr",
78-
"::std::weak_ptr", "::std::auto_ptr"),
79-
templateArgumentCountIs(1))))));
77+
anyOf(allOf(hasAnyName("::std::shared_ptr", "::std::weak_ptr",
78+
"::std::auto_ptr"),
79+
templateArgumentCountIs(1)),
80+
allOf(hasName("::std::unique_ptr"),
81+
templateArgumentCountIs(2))))))));
8082

8183
// We will warn only if the class has a pointer or a C array field which
8284
// probably causes a problem during self-assignment (e.g. first resetting

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ Changes in existing checks
233233
`bsl::optional` and `bdlb::NullableValue` from
234234
<https://github.com/bloomberg/bde>_.
235235

236+
- Improved :doc:`bugprone-unhandled-self-assignment
237+
<clang-tidy/checks/bugprone/unhandled-self-assignment>` check by fixing smart
238+
pointer check against std::unique_ptr type.
239+
236240
- Improved :doc:`bugprone-unsafe-functions
237241
<clang-tidy/checks/bugprone/unsafe-functions>` check to allow specifying
238242
additional functions to match.

clang-tools-extra/test/clang-tidy/checkers/bugprone/unhandled-self-assignment.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ template <class T>
1010
T &&move(T &x) {
1111
}
1212

13-
template <class T>
13+
template <typename T> class default_delete {};
14+
15+
template <class T, typename Deleter = std::default_delete<T>>
1416
class unique_ptr {
1517
};
1618

0 commit comments

Comments
 (0)