https://github.com/higher-performance created https://github.com/llvm/llvm-project/pull/114255
These need to be handled similarly to the standard smart pointers; they all have a `reset` method. >From 2d907aa6453b43a2bb537cf1eb32d7f3f8a850a7 Mon Sep 17 00:00:00 2001 From: higher-performance <higher.performance.git...@gmail.com> Date: Wed, 30 Oct 2024 12:01:00 -0400 Subject: [PATCH] Extend bugprone-use-after-move check to handle std::optional::reset() and std::any::reset() similarly to smart pointers --- .../clang-tidy/bugprone/UseAfterMoveCheck.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp index 8f4b5e8092ddaa..0758707a9ee339 100644 --- a/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp @@ -242,7 +242,7 @@ void UseAfterMoveFinder::getUsesAndReinits( }); } -bool isStandardSmartPointer(const ValueDecl *VD) { +bool isStandardResettable(const ValueDecl *VD) { const Type *TheType = VD->getType().getNonReferenceType().getTypePtrOrNull(); if (!TheType) return false; @@ -256,7 +256,8 @@ bool isStandardSmartPointer(const ValueDecl *VD) { return false; StringRef Name = ID->getName(); - if (Name != "unique_ptr" && Name != "shared_ptr" && Name != "weak_ptr") + if (Name != "unique_ptr" && Name != "shared_ptr" && Name != "weak_ptr" && Name != "optional" && + Name != "any") return false; return RecordDecl->getDeclContext()->isStdNamespace(); @@ -279,7 +280,7 @@ void UseAfterMoveFinder::getDeclRefs( if (DeclRef && BlockMap->blockContainingStmt(DeclRef) == Block) { // Ignore uses of a standard smart pointer that don't dereference the // pointer. - if (Operator || !isStandardSmartPointer(DeclRef->getDecl())) { + if (Operator || !isStandardResettable(DeclRef->getDecl())) { DeclRefs->insert(DeclRef); } } @@ -315,9 +316,10 @@ void UseAfterMoveFinder::getReinits( "::std::unordered_map", "::std::unordered_multiset", "::std::unordered_multimap")))))); - auto StandardSmartPointerTypeMatcher = hasType(hasUnqualifiedDesugaredType( + auto StandardResettableTypeMatcher = hasType(hasUnqualifiedDesugaredType( recordType(hasDeclaration(cxxRecordDecl(hasAnyName( - "::std::unique_ptr", "::std::shared_ptr", "::std::weak_ptr")))))); + "::std::unique_ptr", "::std::shared_ptr", "::std::weak_ptr", "::std::optional", + "::std::any")))))); // Matches different types of reinitialization. auto ReinitMatcher = @@ -340,7 +342,7 @@ void UseAfterMoveFinder::getReinits( callee(cxxMethodDecl(hasAnyName("clear", "assign")))), // reset() on standard smart pointers. cxxMemberCallExpr( - on(expr(DeclRefMatcher, StandardSmartPointerTypeMatcher)), + on(expr(DeclRefMatcher, StandardResettableTypeMatcher)), callee(cxxMethodDecl(hasName("reset")))), // Methods that have the [[clang::reinitializes]] attribute. cxxMemberCallExpr( _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits