Skip to content

Commit 29e4606

Browse files
[clang-tidy] Skip template ctors in modernize-use-equals-default
Skip template ctors in modernize-use-equals-default, such constructors may be enabled/disabled via SFINAE, it is not safe to make them "= default". Test plan: ninja check-all Differential revision: https://reviews.llvm.org/D136797
1 parent c8eb932 commit 29e4606

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ void UseEqualsDefaultCheck::registerMatchers(MatchFinder *Finder) {
241241
this);
242242
Finder->addMatcher(
243243
cxxConstructorDecl(
244-
unless(hasParent(IsUnionLikeClass)), isDefinition(),
244+
unless(
245+
hasParent(decl(anyOf(IsUnionLikeClass, functionTemplateDecl())))),
246+
isDefinition(),
245247
anyOf(
246248
// Default constructor.
247249
allOf(unless(hasAnyConstructorInitializer(isWritten())),
@@ -257,8 +259,9 @@ void UseEqualsDefaultCheck::registerMatchers(MatchFinder *Finder) {
257259
this);
258260
// Copy-assignment operator.
259261
Finder->addMatcher(
260-
cxxMethodDecl(unless(hasParent(IsUnionLikeClass)), isDefinition(),
261-
isCopyAssignmentOperator(),
262+
cxxMethodDecl(unless(hasParent(
263+
decl(anyOf(IsUnionLikeClass, functionTemplateDecl())))),
264+
isDefinition(), isCopyAssignmentOperator(),
262265
// isCopyAssignmentOperator() allows the parameter to be
263266
// passed by value, and in this case it cannot be
264267
// defaulted.

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ Changes in existing checks
158158
The check now skips unions/union-like classes since in this case a default constructor
159159
with empty body is not equivalent to the explicitly defaulted one, variadic constructors
160160
since they cannot be explicitly defaulted. The check also skips copy assignment operators
161-
with nonstandard return types, private/protected default constructors for C++17 or earlier.
162-
The automatic fixit has been adjusted to avoid adding superfluous semicolon.
163-
The check is restricted to C++11 or later.
161+
with nonstandard return types, template constructors, private/protected default constructors
162+
for C++17 or earlier. The automatic fixit has been adjusted to avoid adding superfluous
163+
semicolon. The check is restricted to C++11 or later.
164164

165165
- Change the default behavior of :doc:`readability-avoid-const-params-in-decls
166166
<clang-tidy/checks/readability/avoid-const-params-in-decls>` to not

clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ struct VA {
5858
VA(...) {}
5959
};
6060

61+
// Skip template constructors.
62+
struct TC {
63+
template <unsigned U>
64+
TC() {}
65+
66+
template <unsigned U>
67+
TC(const TC &) {}
68+
69+
template <unsigned U>
70+
TC& operator = (const TC &) { return *this; }
71+
};
72+
6173
// Initializer or arguments.
6274
class IA {
6375
public:

0 commit comments

Comments
 (0)