Skip to content

Commit eda3e96

Browse files
authored
[clang-tidy] Fix false positives in bugprone-crtp-constructor-accessibility check (#132543)
Fix false positives in `bugprone-crtp-constructor-accessibility` check on deleted constructors that cannot be used to construct objects, even if they have public or protected access. Closes #131737.
1 parent 7af14e5 commit eda3e96

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void CrtpConstructorAccessibilityCheck::check(
157157
}
158158

159159
for (auto &&Ctor : CRTPDeclaration->ctors()) {
160-
if (Ctor->getAccess() == AS_private)
160+
if (Ctor->getAccess() == AS_private || Ctor->isDeleted())
161161
continue;
162162

163163
const bool IsPublic = Ctor->getAccess() == AS_public;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ New check aliases
148148
Changes in existing checks
149149
^^^^^^^^^^^^^^^^^^^^^^^^^^
150150

151+
- Improved :doc:`bugprone-crtp-constructor-accessibility
152+
<clang-tidy/checks/bugprone/crtp-constructor-accessibility>` check by fixing
153+
false positives on deleted constructors that cannot be used to construct
154+
objects, even if they have public or protected access.
155+
151156
- Improved :doc:`bugprone-optional-value-conversion
152157
<clang-tidy/checks/bugprone/optional-value-conversion>` check to detect
153158
conversion in argument of ``std::make_optional``.

clang-tools-extra/docs/clang-tidy/checks/bugprone/crtp-constructor-accessibility.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ Example:
6565
To ensure that no accidental instantiation happens, the best practice is to
6666
make the constructor private and declare the derived class as friend. Note
6767
that as a tradeoff, this also gives the derived class access to every other
68-
private members of the CRTP.
68+
private members of the CRTP. However, constructors can still be public or
69+
protected if they are deleted.
6970

7071
Example:
7172

clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,34 @@ void foo() {
253253
(void) A;
254254
}
255255
} // namespace no_warning_unsupported
256+
257+
namespace public_copy_move_constructors_deleted {
258+
template <typename T>
259+
class CRTP
260+
{
261+
CRTP() = default;
262+
friend T;
263+
public:
264+
CRTP(const CRTP&) = delete;
265+
CRTP(CRTP&&) = delete;
266+
};
267+
268+
class A : CRTP<A> {};
269+
270+
} // namespace public_copy_move_constructors_deleted
271+
272+
namespace public_copy_protected_move_constructor_deleted {
273+
template <typename T>
274+
class CRTP
275+
{
276+
CRTP() = default;
277+
friend T;
278+
public:
279+
CRTP(const CRTP&) = delete;
280+
protected:
281+
CRTP(CRTP&&) = delete;
282+
};
283+
284+
class A : CRTP<A> {};
285+
286+
} // namespace public_copy_protected_move_constructor_deleted

0 commit comments

Comments
 (0)