Skip to content

[clang-tidy] Add flag to specify an alternative to std::forward #138755

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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void MissingStdForwardCheck::registerMatchers(MatchFinder *Finder) {
equalsBoundNode("param"), equalsBoundNode("var")))))),
CapturedInLambda)),
callee(unresolvedLookupExpr(hasAnyDeclaration(
namedDecl(hasUnderlyingDecl(hasName("::std::forward")))))),
namedDecl(hasUnderlyingDecl(hasName(ForwardFunction)))))),

unless(anyOf(hasAncestor(typeLoc()),
hasAncestor(expr(hasUnevaluatedContext())))));
Expand Down Expand Up @@ -153,4 +153,13 @@ void MissingStdForwardCheck::check(const MatchFinder::MatchResult &Result) {
<< Param;
}

MissingStdForwardCheck::MissingStdForwardCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
ForwardFunction(Options.get("ForwardFunction", "::std::forward")) {}

void MissingStdForwardCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "ForwardFunction", ForwardFunction);
}

} // namespace clang::tidy::cppcoreguidelines
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ namespace clang::tidy::cppcoreguidelines {
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/missing-std-forward.html
class MissingStdForwardCheck : public ClangTidyCheck {
public:
MissingStdForwardCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
MissingStdForwardCheck(StringRef Name, ClangTidyContext *Context);
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
Expand All @@ -31,6 +30,10 @@ class MissingStdForwardCheck : public ClangTidyCheck {
std::optional<TraversalKind> getCheckTraversalKind() const override {
return TK_IgnoreUnlessSpelledInSource;
}
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;

private:
const StringRef ForwardFunction;
};

} // namespace clang::tidy::cppcoreguidelines
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ Changes in existing checks
<clang-tidy/checks/cert/err33-c>` check by fixing false positives when
a function name is just prefixed with a targeted function name.

- Improved :doc:`cppcoreguidelines-missing-std-forward
<clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by adding a
flag to specify the function used for forwarding instead of ``std::forward``.

- Improved :doc:`misc-const-correctness
<clang-tidy/checks/misc/const-correctness>` check by adding the option
`AllowedTypes`, that excludes specified types from const-correctness
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ Example:
f(1, 2); // Incorrect - may not invoke the desired qualified function operator
}

Options
-------

.. option:: ForwardFunction

Specify the function used for forwarding.
Default is `::std::forward`.

This check implements `F.19
<http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-forward>`_
from the C++ Core Guidelines.
Loading