48 lines · c
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MOVEFORWARDINGREFERENCECHECK_H10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MOVEFORWARDINGREFERENCECHECK_H11 12#include "../ClangTidyCheck.h"13 14namespace clang::tidy::bugprone {15 16/// The check warns if std::move is applied to a forwarding reference (i.e. an17/// rvalue reference of a function template argument type).18///19/// If a developer is unaware of the special rules for template argument20/// deduction on forwarding references, it will seem reasonable to apply21/// std::move to the forwarding reference, in the same way that this would be22/// done for a "normal" rvalue reference.23///24/// This has a consequence that is usually unwanted and possibly surprising: if25/// the function that takes the forwarding reference as its parameter is called26/// with an lvalue, that lvalue will be moved from (and hence placed into an27/// indeterminate state) even though no std::move was applied to the lvalue at28/// the call site.29//30/// The check suggests replacing the std::move with a std::forward.31///32/// For the user-facing documentation see:33/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/move-forwarding-reference.html34class MoveForwardingReferenceCheck : public ClangTidyCheck {35public:36 MoveForwardingReferenceCheck(StringRef Name, ClangTidyContext *Context)37 : ClangTidyCheck(Name, Context) {}38 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {39 return LangOpts.CPlusPlus11;40 }41 void registerMatchers(ast_matchers::MatchFinder *Finder) override;42 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;43};44 45} // namespace clang::tidy::bugprone46 47#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MOVEFORWARDINGREFERENCECHECK_H48