Skip to content

Commit 0f9e1e3

Browse files
committed
[clang-tidy]: fix false positive of cert-oop54-cpp check.
Summary: It seems we need a different matcher for binary operator in a template context. Fixes this issue: https://bugs.llvm.org/show_bug.cgi?id=44499 Reviewers: aaron.ballman, alexfh, hokein, njames93 Reviewed By: aaron.ballman Subscribers: xazax.hun, cfe-commits Tags: #clang, #clang-tools-extra Differential Revision: https://reviews.llvm.org/D76990
1 parent 2e98397 commit 0f9e1e3

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ void UnhandledSelfAssignmentCheck::registerMatchers(MatchFinder *Finder) {
4040

4141
// Self-check: Code compares something with 'this' pointer. We don't check
4242
// whether it is actually the parameter what we compare.
43-
const auto HasNoSelfCheck = cxxMethodDecl(unless(
43+
const auto HasNoSelfCheck = cxxMethodDecl(unless(anyOf(
4444
hasDescendant(binaryOperator(hasAnyOperatorName("==", "!="),
45-
has(ignoringParenCasts(cxxThisExpr()))))));
45+
has(ignoringParenCasts(cxxThisExpr())))),
46+
hasDescendant(cxxOperatorCallExpr(
47+
hasAnyOverloadedOperatorName("==", "!="), argumentCountIs(2),
48+
has(ignoringParenCasts(cxxThisExpr())))))));
4649

4750
// Both copy-and-swap and copy-and-move method creates a copy first and
4851
// assign it to 'this' with swap or move.

clang-tools-extra/test/clang-tidy/checkers/bugprone-unhandled-self-assignment.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,21 @@ class WrongTemplateCopyAndMove {
212212
T *p;
213213
};
214214

215+
// https://bugs.llvm.org/show_bug.cgi?id=44499
216+
class Foo2;
217+
template <int a>
218+
bool operator!=(Foo2 &, Foo2 &) {
219+
class Bar2 {
220+
Bar2 &operator=(const Bar2 &other) {
221+
// CHECK-MESSAGES: [[@LINE-1]]:11: warning: operator=() does not handle self-assignment properly [bugprone-unhandled-self-assignment]
222+
p = other.p;
223+
return *this;
224+
}
225+
226+
int *p;
227+
};
228+
}
229+
215230
///////////////////////////////////////////////////////////////////
216231
/// Test cases correctly ignored by the check.
217232

@@ -283,6 +298,21 @@ class TemplateSelfCheck {
283298
T *p;
284299
};
285300

301+
// https://bugs.llvm.org/show_bug.cgi?id=44499
302+
class Foo;
303+
template <int a>
304+
bool operator!=(Foo &, Foo &) {
305+
class Bar {
306+
Bar &operator=(const Bar &other) {
307+
if (this != &other) {
308+
}
309+
return *this;
310+
}
311+
312+
int *p;
313+
};
314+
}
315+
286316
// There is no warning if the copy assignment operator gets the object by value.
287317
class PassedByValue {
288318
public:

0 commit comments

Comments
 (0)