Skip to content

Commit 311bc80

Browse files
committed
[clang-tidy] Fix false positives with template in misc-unconventional-assign-operator check
1 parent eed98e1 commit 311bc80

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
@@ -208,6 +208,11 @@ Changes in existing checks
208208
<clang-tidy/checks/misc/unused-using-decls>` check by fixing false positives
209209
on ``operator""`` with template parameters.
210210

211+
- Improved :doc:`misc-unconventional-assign-operator
212+
<clang-tidy/checks/misc/misc-unconventional-assign-operator>` check by fixing
213+
false positives when copy assignment operator function in a template class
214+
returns the result of another assignment to ``*this``(``return *this=...``).
215+
211216
- Improved :doc:`misc-use-internal-linkage
212217
<clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives
213218
for function or variable in header file which contains macro expansion and

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 issue143237 {
168+
template<typename T>
169+
struct B {
170+
explicit B(int) {
171+
}
172+
173+
B& operator=(int n) {
174+
// No warning
175+
return *this = B(n);
176+
}
177+
};
178+
}

0 commit comments

Comments
 (0)