Skip to content

Commit 16b3e43

Browse files
authored
[clang-tidy] Ignore non-forwarded arguments if they are unused (#87832)
1 parent eb26edb commit 16b3e43

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include "MissingStdForwardCheck.h"
1010
#include "../utils/Matchers.h"
1111
#include "clang/AST/ASTContext.h"
12-
#include "clang/AST/ExprConcepts.h"
1312
#include "clang/ASTMatchers/ASTMatchFinder.h"
13+
#include "clang/Basic/IdentifierTable.h"
1414

1515
using namespace clang::ast_matchers;
1616

@@ -79,6 +79,11 @@ AST_MATCHER_P(LambdaExpr, hasCaptureDefaultKind, LambdaCaptureDefault, Kind) {
7979
return Node.getCaptureDefault() == Kind;
8080
}
8181

82+
AST_MATCHER(VarDecl, hasIdentifier) {
83+
const IdentifierInfo *ID = Node.getIdentifier();
84+
return ID != NULL && !ID->isPlaceholder();
85+
}
86+
8287
} // namespace
8388

8489
void MissingStdForwardCheck::registerMatchers(MatchFinder *Finder) {
@@ -125,12 +130,14 @@ void MissingStdForwardCheck::registerMatchers(MatchFinder *Finder) {
125130
hasAncestor(expr(hasUnevaluatedContext())))));
126131

127132
Finder->addMatcher(
128-
parmVarDecl(parmVarDecl().bind("param"), isTemplateTypeParameter(),
129-
hasAncestor(functionDecl().bind("func")),
130-
hasAncestor(functionDecl(
131-
isDefinition(), equalsBoundNode("func"), ToParam,
132-
unless(anyOf(isDeleted(), hasDescendant(std::move(
133-
ForwardCallMatcher))))))),
133+
parmVarDecl(
134+
parmVarDecl().bind("param"), hasIdentifier(),
135+
unless(hasAttr(attr::Kind::Unused)), isTemplateTypeParameter(),
136+
hasAncestor(functionDecl().bind("func")),
137+
hasAncestor(functionDecl(
138+
isDefinition(), equalsBoundNode("func"), ToParam,
139+
unless(anyOf(isDeleted(),
140+
hasDescendant(std::move(ForwardCallMatcher))))))),
134141
this);
135142
}
136143

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,9 @@ Changes in existing checks
179179

180180
- Improved :doc:`cppcoreguidelines-missing-std-forward
181181
<clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by no longer
182-
giving false positives for deleted functions and fix false negative when some
183-
parameters are forwarded, but other aren't.
182+
giving false positives for deleted functions, by fixing false negatives when only
183+
a few parameters are forwarded and by ignoring parameters without a name (unused
184+
arguments).
184185

185186
- Improved :doc:`cppcoreguidelines-owning-memory
186187
<clang-tidy/checks/cppcoreguidelines/owning-memory>` check to properly handle

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,16 @@ struct S {
198198
};
199199

200200
} // namespace deleted_functions
201+
202+
namespace unused_arguments {
203+
204+
template<typename F>
205+
void unused_argument1(F&&) {}
206+
207+
template<typename F>
208+
void unused_argument2([[maybe_unused]] F&& f) {}
209+
210+
template<typename F>
211+
void unused_argument3(F&& _) {}
212+
213+
} // namespace unused_arguments

0 commit comments

Comments
 (0)