clang-tools 10.0.0

MoveForwardingReferenceCheck.h
Go to the documentation of this file.
1 //===--- MoveForwardingReferenceCheck.h - clang-tidy ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MOVEFORWARDINGREFERENCECHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MOVEFORWARDINGREFERENCECHECK_H
11
12 #include "../ClangTidyCheck.h"
13
14 namespace clang {
15 namespace tidy {
16 namespace bugprone {
17
18 /// The check warns if std::move is applied to a forwarding reference (i.e. an
19 /// rvalue reference of a function template argument type).
20 ///
21 /// If a developer is unaware of the special rules for template argument
22 /// deduction on forwarding references, it will seem reasonable to apply
23 /// std::move to the forwarding reference, in the same way that this would be
24 /// done for a "normal" rvalue reference.
25 ///
26 /// This has a consequence that is usually unwanted and possibly surprising: if
27 /// the function that takes the forwarding reference as its parameter is called
28 /// with an lvalue, that lvalue will be moved from (and hence placed into an
29 /// indeterminate state) even though no std::move was applied to the lvalue at
30 /// the call site.
31 //
32 /// The check suggests replacing the std::move with a std::forward.
33 ///
34 /// For the user-facing documentation see:
35 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-move-forwarding-reference.html
36 class MoveForwardingReferenceCheck : public ClangTidyCheck {
37 public:
38 MoveForwardingReferenceCheck(StringRef Name, ClangTidyContext *Context)
39 : ClangTidyCheck(Name, Context) {}
40 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
41 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
42 };
43
44 } // namespace bugprone
45 } // namespace tidy
46 } // namespace clang
47
48 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MOVEFORWARDINGREFERENCECHECK_H
clang::tidy::bugprone::MoveForwardingReferenceCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: MoveForwardingReferenceCheck.cpp:95
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::bugprone::MoveForwardingReferenceCheck::MoveForwardingReferenceCheck
MoveForwardingReferenceCheck(StringRef Name, ClangTidyContext *Context)
Definition: MoveForwardingReferenceCheck.h:38
clang::tidy::bugprone::MoveForwardingReferenceCheck
The check warns if std::move is applied to a forwarding reference (i.e.
Definition: MoveForwardingReferenceCheck.h:36
clang::tidy::bugprone::MoveForwardingReferenceCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: MoveForwardingReferenceCheck.cpp:69
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77