Skip to content

Commit b6d3a40

Browse files
Add flag to specify an alternative to std::forward
Since std::forward is nothing more than a cast, part of STL and not the language itself, it's easy to provide a custom implementation if one wishes not to include the entirety of <utility>. Added flag (ForwardFunction) provides a way to continue using this essential check even with the custom implementation of forwarding.
1 parent 529508c commit b6d3a40

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void MissingStdForwardCheck::registerMatchers(MatchFinder *Finder) {
120120
equalsBoundNode("param"), equalsBoundNode("var")))))),
121121
CapturedInLambda)),
122122
callee(unresolvedLookupExpr(hasAnyDeclaration(
123-
namedDecl(hasUnderlyingDecl(hasName("::std::forward")))))),
123+
namedDecl(hasUnderlyingDecl(hasName(ForwardFunction)))))),
124124

125125
unless(anyOf(hasAncestor(typeLoc()),
126126
hasAncestor(expr(hasUnevaluatedContext())))));
@@ -149,4 +149,13 @@ void MissingStdForwardCheck::check(const MatchFinder::MatchResult &Result) {
149149
<< Param;
150150
}
151151

152+
MissingStdForwardCheck::MissingStdForwardCheck(StringRef Name,
153+
ClangTidyContext *Context)
154+
: ClangTidyCheck(Name, Context),
155+
ForwardFunction(Options.get("ForwardFunction", "::std::forward")) {}
156+
157+
void MissingStdForwardCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
158+
Options.store(Opts, "ForwardFunction", ForwardFunction);
159+
}
160+
152161
} // namespace clang::tidy::cppcoreguidelines

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ namespace clang::tidy::cppcoreguidelines {
2121
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/missing-std-forward.html
2222
class MissingStdForwardCheck : public ClangTidyCheck {
2323
public:
24-
MissingStdForwardCheck(StringRef Name, ClangTidyContext *Context)
25-
: ClangTidyCheck(Name, Context) {}
24+
MissingStdForwardCheck(StringRef Name, ClangTidyContext *Context);
2625
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2726
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
2827
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
@@ -31,6 +30,10 @@ class MissingStdForwardCheck : public ClangTidyCheck {
3130
std::optional<TraversalKind> getCheckTraversalKind() const override {
3231
return TK_IgnoreUnlessSpelledInSource;
3332
}
33+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
34+
35+
private:
36+
const StringRef ForwardFunction;
3437
};
3538

3639
} // namespace clang::tidy::cppcoreguidelines

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ Changes in existing checks
327327
<clang-tidy/checks/readability/redundant-smartptr-get>` check by fixing
328328
some false positives involving smart pointers to arrays.
329329

330+
- Improved :doc:`cppcoreguidelines-missing-std-forward
331+
<clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by adding a
332+
flag to specify the function used for forwarding instead of ``std::forward``.
333+
330334
Removed checks
331335
^^^^^^^^^^^^^^
332336

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/missing-std-forward.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ Example:
3535
f(1, 2); // Incorrect - may not invoke the desired qualified function operator
3636
}
3737

38+
Options
39+
-------
40+
41+
.. option:: ForwardFunction
42+
43+
Specify the function used for forwarding.
44+
Default is `::std::forward`.
45+
3846
This check implements `F.19
3947
<http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-forward>`_
4048
from the C++ Core Guidelines.

0 commit comments

Comments
 (0)