[clang-tools-extra] [clang-tidy] Ignore non-forwarded arguments if they are unnamed (PR #87832)

Danny Mösch via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 8 10:55:31 PDT 2024


https://github.com/SimplyDanny updated https://github.com/llvm/llvm-project/pull/87832

>From 229e1cb251139a32fc2e944b8d0f4d402987820f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Danny=20M=C3=B6sch?= <danny.moesch at icloud.com>
Date: Fri, 5 Apr 2024 23:41:50 +0200
Subject: [PATCH 1/5] [clang-tidy] Ignore non-forwarded arguments if they are
 unnamed

---
 .../cppcoreguidelines/MissingStdForwardCheck.cpp  | 15 +++++++++------
 clang-tools-extra/docs/ReleaseNotes.rst           |  4 ++++
 .../cppcoreguidelines/missing-std-forward.cpp     |  3 +++
 3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
index 87fd8adf997082..6817bcb24c8112 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
@@ -79,6 +79,8 @@ AST_MATCHER_P(LambdaExpr, hasCaptureDefaultKind, LambdaCaptureDefault, Kind) {
   return Node.getCaptureDefault() == Kind;
 }
 
+AST_MATCHER(ParmVarDecl, hasAnyName) { return !Node.getName().empty(); }
+
 } // namespace
 
 void MissingStdForwardCheck::registerMatchers(MatchFinder *Finder) {
@@ -125,12 +127,13 @@ void MissingStdForwardCheck::registerMatchers(MatchFinder *Finder) {
                    hasAncestor(expr(hasUnevaluatedContext())))));
 
   Finder->addMatcher(
-      parmVarDecl(parmVarDecl().bind("param"), isTemplateTypeParameter(),
-                  hasAncestor(functionDecl().bind("func")),
-                  hasAncestor(functionDecl(
-                      isDefinition(), equalsBoundNode("func"), ToParam,
-                      unless(anyOf(isDeleted(), hasDescendant(std::move(
-                                                    ForwardCallMatcher))))))),
+      parmVarDecl(
+          parmVarDecl().bind("param"), hasAnyName(), isTemplateTypeParameter(),
+          hasAncestor(functionDecl().bind("func")),
+          hasAncestor(functionDecl(
+              isDefinition(), equalsBoundNode("func"), ToParam,
+              unless(anyOf(isDeleted(),
+                           hasDescendant(std::move(ForwardCallMatcher))))))),
       this);
 }
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index bdd53f06e7e2f8..51143bfce927dd 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -182,6 +182,10 @@ Changes in existing checks
   giving false positives for deleted functions and fix false negative when some
   parameters are forwarded, but other aren't.
 
+- Improved :doc:`cppcoreguidelines-missing-std-forward
+  <clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by ignoring
+  parameters without a name (unused arguments).
+
 - Improved :doc:`cppcoreguidelines-owning-memory
   <clang-tidy/checks/cppcoreguidelines/owning-memory>` check to properly handle
   return type in lambdas and in nested functions.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp
index 9a50eabf619bd5..c435a0e023ba0b 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp
@@ -198,3 +198,6 @@ struct S {
 };
 
 } // namespace deleted_functions
+
+template<typename F>
+void unused_argument(F&&) {}

>From 563d454a3843e94bc7a1ef466c672aff4d9f24c6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Danny=20M=C3=B6sch?= <danny.moesch at icloud.com>
Date: Sat, 6 Apr 2024 16:51:36 +0200
Subject: [PATCH 2/5] Merge entries in release notes

---
 clang-tools-extra/docs/ReleaseNotes.rst | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 51143bfce927dd..a7193e90c38da2 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -179,12 +179,9 @@ Changes in existing checks
 
 - Improved :doc:`cppcoreguidelines-missing-std-forward
   <clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by no longer
-  giving false positives for deleted functions and fix false negative when some
-  parameters are forwarded, but other aren't.
-
-- Improved :doc:`cppcoreguidelines-missing-std-forward
-  <clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by ignoring
-  parameters without a name (unused arguments).
+  giving false positives for deleted functions, by fixing false negatives when only
+  a few parameters are forwarded and by ignoring parameters without a name (unused
+  arguments).
 
 - Improved :doc:`cppcoreguidelines-owning-memory
   <clang-tidy/checks/cppcoreguidelines/owning-memory>` check to properly handle

>From a0f8a9074e2b33d3dfcf92179d79b8c907dde7b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Danny=20M=C3=B6sch?= <danny.moesch at icloud.com>
Date: Sun, 7 Apr 2024 13:49:41 +0200
Subject: [PATCH 3/5] Check identifier instead of name

---
 .../cppcoreguidelines/MissingStdForwardCheck.cpp           | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
index 6817bcb24c8112..4546b49c3d8766 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
@@ -9,7 +9,6 @@
 #include "MissingStdForwardCheck.h"
 #include "../utils/Matchers.h"
 #include "clang/AST/ASTContext.h"
-#include "clang/AST/ExprConcepts.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 
 using namespace clang::ast_matchers;
@@ -79,7 +78,7 @@ AST_MATCHER_P(LambdaExpr, hasCaptureDefaultKind, LambdaCaptureDefault, Kind) {
   return Node.getCaptureDefault() == Kind;
 }
 
-AST_MATCHER(ParmVarDecl, hasAnyName) { return !Node.getName().empty(); }
+AST_MATCHER(VarDecl, hasIdentifier) { return Node.getIdentifier() != NULL; }
 
 } // namespace
 
@@ -128,8 +127,8 @@ void MissingStdForwardCheck::registerMatchers(MatchFinder *Finder) {
 
   Finder->addMatcher(
       parmVarDecl(
-          parmVarDecl().bind("param"), hasAnyName(), isTemplateTypeParameter(),
-          hasAncestor(functionDecl().bind("func")),
+          parmVarDecl().bind("param"), hasIdentifier(),
+          isTemplateTypeParameter(), hasAncestor(functionDecl().bind("func")),
           hasAncestor(functionDecl(
               isDefinition(), equalsBoundNode("func"), ToParam,
               unless(anyOf(isDeleted(),

>From d0138b4d0ceae4f4ca52d9a4a60897d4fd350e7e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Danny=20M=C3=B6sch?= <danny.moesch at icloud.com>
Date: Sun, 7 Apr 2024 13:50:43 +0200
Subject: [PATCH 4/5] Take [[maybe_unused]] attribute into account

---
 .../cppcoreguidelines/MissingStdForwardCheck.cpp         | 3 ++-
 .../checkers/cppcoreguidelines/missing-std-forward.cpp   | 9 ++++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
index 4546b49c3d8766..9a16d886ca191f 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
@@ -128,7 +128,8 @@ void MissingStdForwardCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
       parmVarDecl(
           parmVarDecl().bind("param"), hasIdentifier(),
-          isTemplateTypeParameter(), hasAncestor(functionDecl().bind("func")),
+          unless(hasAttr(attr::Kind::Unused)), isTemplateTypeParameter(),
+          hasAncestor(functionDecl().bind("func")),
           hasAncestor(functionDecl(
               isDefinition(), equalsBoundNode("func"), ToParam,
               unless(anyOf(isDeleted(),
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp
index c435a0e023ba0b..5e7f684c08381c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp
@@ -199,5 +199,12 @@ struct S {
 
 } // namespace deleted_functions
 
+namespace unused_arguments {
+
+template<typename F>
+void unused_argument1(F&&) {}
+
 template<typename F>
-void unused_argument(F&&) {}
+void unused_argument2([[maybe_unused]] F&& f) {}
+
+} // namespace unused_arguments

>From 0cb8672cdb9d2cfc154d1fbab889b0c45075d3b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Danny=20M=C3=B6sch?= <danny.moesch at icloud.com>
Date: Sun, 7 Apr 2024 19:47:30 +0200
Subject: [PATCH 5/5] Ignore placeholder arguments

---
 .../clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp | 6 +++++-
 .../checkers/cppcoreguidelines/missing-std-forward.cpp      | 3 +++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
index 9a16d886ca191f..bbb35228ce47fb 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
@@ -10,6 +10,7 @@
 #include "../utils/Matchers.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Basic/IdentifierTable.h"
 
 using namespace clang::ast_matchers;
 
@@ -78,7 +79,10 @@ AST_MATCHER_P(LambdaExpr, hasCaptureDefaultKind, LambdaCaptureDefault, Kind) {
   return Node.getCaptureDefault() == Kind;
 }
 
-AST_MATCHER(VarDecl, hasIdentifier) { return Node.getIdentifier() != NULL; }
+AST_MATCHER(VarDecl, hasIdentifier) {
+  const IdentifierInfo *ID = Node.getIdentifier();
+  return ID != NULL && !ID->isPlaceholder();
+}
 
 } // namespace
 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp
index 5e7f684c08381c..8116db58c937d4 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp
@@ -207,4 +207,7 @@ void unused_argument1(F&&) {}
 template<typename F>
 void unused_argument2([[maybe_unused]] F&& f) {}
 
+template<typename F>
+void unused_argument3(F&& _) {}
+
 } // namespace unused_arguments



More information about the cfe-commits mailing list