Skip to content

Commit 70fdd1d

Browse files
committed
reflow check() implementation and add another unit test
1 parent 4e516c0 commit 70fdd1d

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

clang-tools-extra/clang-tidy/bugprone/MoveSharedPointerContentsCheck.cpp

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <iostream>
10+
911
#include "MoveSharedPointerContentsCheck.h"
1012
#include "../ClangTidyCheck.h"
1113
#include "../utils/Matchers.h"
@@ -52,15 +54,15 @@ void MoveSharedPointerContentsCheck::registerMatchers(MatchFinder *Finder) {
5254

5355
auto resolvedType = callExpr(anyOf(
5456
// Resolved type, direct move.
55-
callExpr(
56-
isStdMove,
57-
hasArgument(
58-
0,
59-
cxxOperatorCallExpr(
60-
hasOverloadedOperatorName("*"),
61-
hasDescendant(declRefExpr(hasType(qualType(isSharedPointer(
62-
matchers::matchesAnyListedName(SharedPointerClasses)))))),
63-
callee(cxxMethodDecl()))))
57+
callExpr(isStdMove,
58+
hasArgument(
59+
0, cxxOperatorCallExpr(
60+
hasOverloadedOperatorName("*"),
61+
hasArgument(
62+
0, declRefExpr(hasType(qualType(isSharedPointer(
63+
matchers::matchesAnyListedName(
64+
SharedPointerClasses)))))),
65+
callee(cxxMethodDecl()))))
6466
.bind("call"),
6567
// Resolved type, move out of get().
6668
callExpr(
@@ -113,30 +115,23 @@ void MoveSharedPointerContentsCheck::registerMatchers(MatchFinder *Finder) {
113115

114116
void MoveSharedPointerContentsCheck::check(
115117
const MatchFinder::MatchResult &Result) {
116-
clang::SourceLocation Loc;
117-
if (const auto *UnresolvedCall =
118-
Result.Nodes.getNodeAs<CallExpr>("unresolved_call");
119-
UnresolvedCall != nullptr) {
120-
Loc = UnresolvedCall->getBeginLoc();
121-
} else if (const auto *UnresolvedGetCall =
122-
Result.Nodes.getNodeAs<CallExpr>("unresolved_get_call");
123-
UnresolvedGetCall != nullptr) {
124-
Loc = UnresolvedGetCall->getBeginLoc();
125-
} else if (const auto *GetCall = Result.Nodes.getNodeAs<CallExpr>("get_call");
126-
GetCall != nullptr) {
127-
Loc = GetCall->getBeginLoc();
128-
} else if (const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
129-
Call != nullptr) {
130-
Loc = Call->getBeginLoc();
131-
} else {
132-
return;
118+
const CallExpr *Call = nullptr;
119+
for (const llvm::StringRef binding :
120+
{"unresolved_call", "unresolved_get_call", "get_call", "call"}) {
121+
if (const auto *C = Result.Nodes.getNodeAs<CallExpr>(binding);
122+
C != nullptr) {
123+
Call = C;
124+
break;
125+
}
133126
}
134127

135-
if (Loc.isValid()) {
136-
diag(Loc,
137-
"don't move the contents out of a shared pointer, as other accessors "
138-
"expect them to remain in a determinate state");
128+
if (Call == nullptr && !Call->getBeginLoc().isValid()) {
129+
return;
139130
}
131+
132+
diag(Call->getBeginLoc(),
133+
"don't move the contents out of a shared pointer, as other accessors "
134+
"expect them to remain in a determinate state");
140135
}
141136

142137
} // namespace clang::tidy::bugprone

clang-tools-extra/test/clang-tidy/checkers/bugprone/move-shared-pointer-contents.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,10 @@ HasASharedPtrField returnsStruct() {
141141
void subfield() {
142142
int x = std::move(returnsStruct().x->x);
143143
}
144+
145+
Nontrivial* dummy(int, std::shared_ptr<Nontrivial>) { return nullptr; }
146+
147+
void sharedPointerIsOtherParameter() {
148+
std::shared_ptr<Nontrivial> p;
149+
Nontrivial n = std::move(*dummy(1, p));
150+
}

0 commit comments

Comments
 (0)