[clang-tools-extra] 0e32508 - [clang-tidy] Support concepts in `bugprone-forwarding-reference-overload`
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Sat Jul 22 12:52:00 PDT 2023
Author: Evgeny Shulgin
Date: 2023-07-22T19:46:31Z
New Revision: 0e325081192be951a9d54bb246ef030c37bffd21
URL: https://github.com/llvm/llvm-project/commit/0e325081192be951a9d54bb246ef030c37bffd21
DIFF: https://github.com/llvm/llvm-project/commit/0e325081192be951a9d54bb246ef030c37bffd21.diff
LOG: [clang-tidy] Support concepts in `bugprone-forwarding-reference-overload`
Ignore constrained perfect forwarding constructors.
Fixes: #58230
Reviewed By: PiotrZSL
Differential Revision: https://reviews.llvm.org/D135476
Added:
clang-tools-extra/test/clang-tidy/checkers/bugprone/forwarding-reference-overload-concepts.cpp
Modified:
clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/bugprone/forwarding-reference-overload.rst
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp
index 99cb1206b24b4e..c608fe713f9f5b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp
@@ -56,6 +56,9 @@ AST_MATCHER_P(TemplateTypeParmDecl, hasDefaultArgument,
return Node.hasDefaultArgument() &&
TypeMatcher.matches(Node.getDefaultArgument(), Finder, Builder);
}
+AST_MATCHER(TemplateDecl, hasAssociatedConstraints) {
+ return Node.hasAssociatedConstraints();
+}
} // namespace
void ForwardingReferenceOverloadCheck::registerMatchers(MatchFinder *Finder) {
@@ -74,6 +77,9 @@ void ForwardingReferenceOverloadCheck::registerMatchers(MatchFinder *Finder) {
// No warning: enable_if as constructor parameter.
parmVarDecl(hasType(isEnableIf())))),
unless(hasParent(functionTemplateDecl(anyOf(
+ // No warning: has associated constraints (like requires
+ // expression).
+ hasAssociatedConstraints(),
// No warning: enable_if as type parameter.
has(templateTypeParmDecl(hasDefaultArgument(isEnableIf()))),
// No warning: enable_if as non-type template parameter.
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 3b4a9488e1206f..2cc0010884a7a0 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -288,6 +288,10 @@ Changes in existing checks
<clang-tidy/checks/bugprone/fold-init-type>` to handle iterators that do not
define `value_type` type aliases.
+- Improved :doc:`bugprone-forwarding-reference-overload
+ <clang-tidy/checks/bugprone/forwarding-reference-overload>` check to ignore
+ constructors with associated constraints (C++ concepts).
+
- Improved :doc:`bugprone-incorrect-roundings
<clang-tidy/checks/bugprone/incorrect-roundings>` check by adding support for
other floating point representations in float constant like ``0.5L``.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/forwarding-reference-overload.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/forwarding-reference-overload.rst
index 37078d498b3301..6d70a50d2e9ca0 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/forwarding-reference-overload.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/forwarding-reference-overload.rst
@@ -34,6 +34,15 @@ Consider the following example:
enable_if_t<is_constructible_v<tuple<string, int>, A&&...>, int> = 0>
explicit Person(A&&... a) {}
+ // C5: perfect forwarding ctor guarded with requires expression
+ template<typename T>
+ requires requires { is_special<T>; }
+ explicit Person(T&& n) {}
+
+ // C6: perfect forwarding ctor guarded with concept requirement
+ template<Special T>
+ explicit Person(T&& n) {}
+
// (possibly compiler generated) copy ctor
Person(const Person& rhs);
};
@@ -42,8 +51,8 @@ The check warns for constructors C1 and C2, because those can hide copy and move
constructors. We suppress warnings if the copy and the move constructors are both
disabled (deleted or private), because there is nothing the perfect forwarding
constructor could hide in this case. We also suppress warnings for constructors
-like C3 and C4 that are guarded with an ``enable_if``, assuming the programmer was
-aware of the possible hiding.
+like C3-C6 that are guarded with an ``enable_if`` or a concept, assuming the
+programmer was aware of the possible hiding.
Background
----------
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/forwarding-reference-overload-concepts.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/forwarding-reference-overload-concepts.cpp
new file mode 100644
index 00000000000000..146fc307a902cb
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/forwarding-reference-overload-concepts.cpp
@@ -0,0 +1,29 @@
+// RUN: %check_clang_tidy -std=c++20-or-later %s bugprone-forwarding-reference-overload %t
+
+template <typename T> constexpr bool just_true = true;
+
+class Test {
+public:
+ template <typename T> Test(T &&n);
+ // CHECK-NOTES: :[[@LINE-1]]:25: warning: constructor accepting a forwarding reference can hide the copy and move constructors
+
+ Test(const Test &rhs);
+ // CHECK-NOTES: :[[@LINE-1]]:3: note: copy constructor declared here
+};
+
+class Test1 {
+public:
+ // Guarded with requires expression.
+ template <typename T>
+ requires requires { just_true<T>; }
+ Test1(T &&n);
+};
+
+template<typename T>
+concept JustTrueConcept = requires { just_true<T>; };
+
+class Test2 {
+public:
+ // Guarded with concept requirement.
+ template <JustTrueConcept T> Test2(T &&n);
+};
More information about the cfe-commits
mailing list