Skip to content

Commit 6c153e5

Browse files
authored
[clang-tidy] Fix false positives with template in misc-unconventional-assign-operator check (#143292)
Fix false positives when copy assignment operator function in a template class returns the result of another assignment to `*this`, this check doesn't consider this situation that there will be a `BinaryOperator` for assignment rather than `CXXOperatorCallExpr` since `this`'s type is dependent. Closes #143237.
1 parent ab92c68 commit 6c153e5

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ void UnconventionalAssignOperatorCheck::registerMatchers(
6666
hasArgument(0, cxxThisExpr())),
6767
cxxOperatorCallExpr(
6868
hasOverloadedOperatorName("="),
69-
hasArgument(
70-
0, unaryOperator(hasOperatorName("*"),
69+
hasArgument(0, unaryOperator(hasOperatorName("*"),
70+
hasUnaryOperand(cxxThisExpr())))),
71+
binaryOperator(
72+
hasOperatorName("="),
73+
hasLHS(unaryOperator(hasOperatorName("*"),
7174
hasUnaryOperand(cxxThisExpr())))))))));
7275
const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType);
7376

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ Changes in existing checks
266266
<clang-tidy/checks/misc/redundant-expression>` check by providing additional
267267
examples and fixing some macro related false positives.
268268

269+
- Improved :doc:`misc-unconventional-assign-operator
270+
<clang-tidy/checks/misc/unconventional-assign-operator>` check by fixing
271+
false positives when copy assignment operator function in a template class
272+
returns the result of another assignment to ``*this`` (``return *this=...``).
273+
269274
- Improved :doc:`misc-unused-using-decls
270275
<clang-tidy/checks/misc/unused-using-decls>` check by fixing false positives
271276
on ``operator""`` with template parameters.

clang-tools-extra/test/clang-tidy/checkers/misc/unconventional-assign-operator.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,16 @@ struct TemplateTypeAlias {
163163
Alias3<TypeAlias::Alias> &operator=(double) { return *this; }
164164
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return 'TemplateTypeAlias&' [misc-unconventional-assign-operator]
165165
};
166+
167+
namespace gh143237 {
168+
template<typename T>
169+
struct TemplateAssignment {
170+
explicit TemplateAssignment(int) {
171+
}
172+
173+
TemplateAssignment& operator=(int n) {
174+
// No warning
175+
return *this = TemplateAssignment(n);
176+
}
177+
};
178+
}

0 commit comments

Comments
 (0)