[clang-tools-extra] Extend bugprone-use-after-move check to handle std::optional::reset() and std::any::reset() (PR #114255)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 30 09:02:43 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
@llvm/pr-subscribers-clang-tools-extra
Author: None (higher-performance)
<details>
<summary>Changes</summary>
These need to be handled similarly to the standard smart pointers; they all have a `reset` method.
---
Full diff: https://github.com/llvm/llvm-project/pull/114255.diff
1 Files Affected:
- (modified) clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp (+8-6)
``````````diff
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(
``````````
</details>
https://github.com/llvm/llvm-project/pull/114255
More information about the cfe-commits
mailing list