1 //===--- UnnecessaryCopyInitialization.h - clang-tidy------------*- C++ -*-===//
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_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
11 
12 #include "../ClangTidyCheck.h"
13 
14 namespace clang {
15 namespace tidy {
16 namespace performance {
17 
18 // The check detects local variable declarations that are copy initialized with
19 // the const reference of a function call or the const reference of a method
20 // call whose object is guaranteed to outlive the variable's scope and suggests
21 // to use a const reference.
22 //
23 // The check currently only understands a subset of variables that are
24 // guaranteed to outlive the const reference returned, namely: const variables,
25 // const references, and const pointers to const.
26 class UnnecessaryCopyInitialization : public ClangTidyCheck {
27 public:
28   UnnecessaryCopyInitialization(StringRef Name, ClangTidyContext *Context);
29   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
30   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
31   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
32 
33 private:
34   void handleCopyFromMethodReturn(const VarDecl &Var, const Stmt &BlockStmt,
35                                   bool IssueFix, const VarDecl *ObjectArg,
36                                   ASTContext &Context);
37   void handleCopyFromLocalVar(const VarDecl &NewVar, const VarDecl &OldVar,
38                               const Stmt &BlockStmt, bool IssueFix,
39                               ASTContext &Context);
40   const std::vector<std::string> AllowedTypes;
41 };
42 
43 } // namespace performance
44 } // namespace tidy
45 } // namespace clang
46 
47 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
48