From c27483763c883ad268ba61249d1c0274a719e2d6 Mon Sep 17 00:00:00 2001 From: flovent <144676429+flovent@users.noreply.github.com> Date: Tue, 7 Jan 2025 20:56:21 +0800 Subject: [PATCH] [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. --- .../clang-tidy/bugprone/UnhandledSelfAssignmentCheck.cpp | 8 +++++--- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ .../checkers/bugprone/unhandled-self-assignment.cpp | 4 +++- 3 files changed, 12 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/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 1fd9b6077be5f5..35cb3e387e4e64 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -233,6 +233,10 @@ Changes in existing checks `bsl::optional` and `bdlb::NullableValue` from _. +- Improved :doc:`bugprone-unhandled-self-assignment + ` check by fixing smart + pointer check against std::unique_ptr type. + - Improved :doc:`bugprone-unsafe-functions ` check to allow specifying additional functions to match. 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 T &&move(T &x) { } -template +template class default_delete {}; + +template > class unique_ptr { };