Skip to content

[clang-tidy] Fix false positives in bugprone-crtp-constructor-accessibility check #132543

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 2 commits into
base: main
Choose a base branch
from

Conversation

vbvictor
Copy link
Contributor

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.

@llvmbot
Copy link
Member

llvmbot commented Mar 22, 2025

@llvm/pr-subscribers-clang-tidy

Author: Baranov Victor (vbvictor)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/132543.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp (+1-1)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+5)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp (+31)
diff --git a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
index 8eaf54fe0088a..28e8fe002d575 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
@@ -157,7 +157,7 @@ void CrtpConstructorAccessibilityCheck::check(
   }
 
   for (auto &&Ctor : CRTPDeclaration->ctors()) {
-    if (Ctor->getAccess() == AS_private)
+    if (Ctor->getAccess() == AS_private || Ctor->isDeleted())
       continue;
 
     const bool IsPublic = Ctor->getAccess() == AS_public;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 72aa05eb4dcd1..4bbe4a693c811 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -127,6 +127,11 @@ New check aliases
 Changes in existing checks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Improved :doc:`bugprone-crtp-constructor-accessibility
+  <clang-tidy/checks/bugprone/crtp-constructor-accessibility>` check by fixing
+  false positives on deleted constructors that cannot be used to construct
+  objects, even if they have public or protected access.
+
 - Improved :doc:`bugprone-optional-value-conversion
   <clang-tidy/checks/bugprone/optional-value-conversion>` check to detect
   conversion in argument of ``std::make_optional``.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
index f33b8457cc8af..44cfcd136f238 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
@@ -253,3 +253,34 @@ void foo() {
     (void) A;
 }
 } // namespace no_warning_unsupported
+
+namespace public_copy_move_constructors_deleted {
+template <typename T>
+class CRTP
+{
+    CRTP() = default;
+    friend T;
+  public:
+    CRTP(const CRTP&) = delete;
+    CRTP(CRTP&&) = delete;
+};
+
+class A : CRTP<A> {};
+
+} // namespace public_copy_move_constructors_deleted
+
+namespace public_copy_protected_move_constructor_deleted {
+template <typename T>
+class CRTP
+{
+    CRTP() = default;
+    friend T;
+  public:
+    CRTP(const CRTP&) = delete;
+  protected:
+    CRTP(CRTP&&) = delete;
+};
+
+class A : CRTP<A> {};
+
+} // namespace public_copy_protected_move_constructor_deleted

@llvmbot
Copy link
Member

llvmbot commented Mar 22, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Baranov Victor (vbvictor)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/132543.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp (+1-1)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+5)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp (+31)
diff --git a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
index 8eaf54fe0088a..28e8fe002d575 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
@@ -157,7 +157,7 @@ void CrtpConstructorAccessibilityCheck::check(
   }
 
   for (auto &&Ctor : CRTPDeclaration->ctors()) {
-    if (Ctor->getAccess() == AS_private)
+    if (Ctor->getAccess() == AS_private || Ctor->isDeleted())
       continue;
 
     const bool IsPublic = Ctor->getAccess() == AS_public;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 72aa05eb4dcd1..4bbe4a693c811 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -127,6 +127,11 @@ New check aliases
 Changes in existing checks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Improved :doc:`bugprone-crtp-constructor-accessibility
+  <clang-tidy/checks/bugprone/crtp-constructor-accessibility>` check by fixing
+  false positives on deleted constructors that cannot be used to construct
+  objects, even if they have public or protected access.
+
 - Improved :doc:`bugprone-optional-value-conversion
   <clang-tidy/checks/bugprone/optional-value-conversion>` check to detect
   conversion in argument of ``std::make_optional``.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
index f33b8457cc8af..44cfcd136f238 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
@@ -253,3 +253,34 @@ void foo() {
     (void) A;
 }
 } // namespace no_warning_unsupported
+
+namespace public_copy_move_constructors_deleted {
+template <typename T>
+class CRTP
+{
+    CRTP() = default;
+    friend T;
+  public:
+    CRTP(const CRTP&) = delete;
+    CRTP(CRTP&&) = delete;
+};
+
+class A : CRTP<A> {};
+
+} // namespace public_copy_move_constructors_deleted
+
+namespace public_copy_protected_move_constructor_deleted {
+template <typename T>
+class CRTP
+{
+    CRTP() = default;
+    friend T;
+  public:
+    CRTP(const CRTP&) = delete;
+  protected:
+    CRTP(CRTP&&) = delete;
+};
+
+class A : CRTP<A> {};
+
+} // namespace public_copy_protected_move_constructor_deleted

Copy link
Contributor

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

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

maybe we should mention it in check's doc also.

@vbvictor
Copy link
Contributor Author

maybe we should mention it in check's doc also.

Added mention of this behavior in docs.

@vbvictor
Copy link
Contributor Author

@HerrCai0907, If you are fine with docs, I'd appreciate merging this PR, thank you

@vbvictor
Copy link
Contributor Author

vbvictor commented Apr 8, 2025

@HerrCai0907, Can you merge this PR please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive for bugprone-crtp-constructor-accessibility
3 participants