Skip to content

Commit

Permalink
[clang-tidy] bugprone-unhandled-self-assignment: fix smart pointer ch…
Browse files Browse the repository at this point in the history
…eck against std::unique_ptr type (llvm#121266)

Unlike other standard smart pointer types, std::unique_ptr has two
template arguments.
testcase need to be updated too.
  • Loading branch information
flovent authored Jan 7, 2025
1 parent 525f526 commit c274837
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
};

Expand Down

0 comments on commit c274837

Please sign in to comment.