-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Extend bugprone-use-after-move check to handle std::optional::reset() and std::any::reset() #114255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: None (higher-performance) ChangesThese need to be handled similarly to the standard smart pointers; they all have a Full diff: https://github.com/llvm/llvm-project/pull/114255.diff 1 Files Affected:
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(
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
8f1bd4e
to
49d0961
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes should be reflected in documentation and Release Notes.
49d0961
to
3861d77
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
clang-tools-extra/docs/clang-tidy/checks/bugprone/use-after-move.rst
Outdated
Show resolved
Hide resolved
3861d77
to
bc8927c
Compare
clang-tools-extra/docs/clang-tidy/checks/bugprone/use-after-move.rst
Outdated
Show resolved
Hide resolved
b42ffbf
to
ae601ed
Compare
clang-tools-extra/docs/clang-tidy/checks/bugprone/use-after-move.rst
Outdated
Show resolved
Hide resolved
ae601ed
to
d73c58a
Compare
clang-tools-extra/docs/clang-tidy/checks/bugprone/use-after-move.rst
Outdated
Show resolved
Hide resolved
d69cabf
to
f5e5148
Compare
… and std::any::reset() similarly to smart pointers
f5e5148
to
873f5a2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks
Thanks! Could we merge this? |
Yes, thanks |
Hm, on mobile it wants to use my email as the commit email... I'll merge the PR later then. |
Thank you |
These need to be handled similarly to the standard smart pointers; they all have a
reset
method.