Skip to content

[clang-tidy] Fix false positives with template in misc-unconventional-assign-operator check #143292

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ void UnconventionalAssignOperatorCheck::registerMatchers(
hasArgument(0, cxxThisExpr())),
cxxOperatorCallExpr(
hasOverloadedOperatorName("="),
hasArgument(
0, unaryOperator(hasOperatorName("*"),
hasArgument(0, unaryOperator(hasOperatorName("*"),
hasUnaryOperand(cxxThisExpr())))),
binaryOperator(
hasOperatorName("="),
hasLHS(unaryOperator(hasOperatorName("*"),
hasUnaryOperand(cxxThisExpr())))))))));
const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType);

Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ Changes in existing checks
<clang-tidy/checks/misc/redundant-expression>` check by providing additional
examples and fixing some macro related false positives.

- Improved :doc:`misc-unconventional-assign-operator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be before misc-unused-using-decls.

<clang-tidy/checks/misc/unconventional-assign-operator>` check by fixing
false positives when copy assignment operator function in a template class
returns the result of another assignment to ``*this``(``return *this=...``).

- Improved :doc:`misc-unused-using-decls
<clang-tidy/checks/misc/unused-using-decls>` check by fixing false positives
on ``operator""`` with template parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,16 @@ struct TemplateTypeAlias {
Alias3<TypeAlias::Alias> &operator=(double) { return *this; }
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return 'TemplateTypeAlias&' [misc-unconventional-assign-operator]
};

namespace issue143237 {
template<typename T>
struct B {
explicit B(int) {
}

B& operator=(int n) {
// No warning
return *this = B(n);
}
};
}