xref: /llvm-project/clang-tools-extra/clang-tidy/performance/NoexceptMoveConstructorCheck.cpp (revision 1d0759e6c3c2eaa8cbacfd76d409ed80ffd9fe9d)
1 //===--- NoexceptMoveConstructorCheck.cpp - 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 #include "NoexceptMoveConstructorCheck.h"
10 #include "clang/ASTMatchers/ASTMatchFinder.h"
11 
12 using namespace clang::ast_matchers;
13 
14 // FixItHint - comment added to fix list.rst generation in add_new_check.py.
15 // Do not remove. Fixes are generated in base class.
16 
17 namespace clang::tidy::performance {
18 
registerMatchers(MatchFinder * Finder)19 void NoexceptMoveConstructorCheck::registerMatchers(MatchFinder *Finder) {
20   Finder->addMatcher(
21       cxxMethodDecl(unless(isDeleted()),
22                     anyOf(cxxConstructorDecl(isMoveConstructor()),
23                           isMoveAssignmentOperator()))
24           .bind(BindFuncDeclName),
25       this);
26 }
27 
reportMissingNoexcept(const FunctionDecl * FuncDecl)28 DiagnosticBuilder NoexceptMoveConstructorCheck::reportMissingNoexcept(
29     const FunctionDecl *FuncDecl) {
30   return diag(FuncDecl->getLocation(),
31               "move %select{assignment operator|constructor}0s should "
32               "be marked noexcept")
33          << CXXConstructorDecl::classof(FuncDecl);
34 }
35 
reportNoexceptEvaluatedToFalse(const FunctionDecl * FuncDecl,const Expr * NoexceptExpr)36 void NoexceptMoveConstructorCheck::reportNoexceptEvaluatedToFalse(
37     const FunctionDecl *FuncDecl, const Expr *NoexceptExpr) {
38   diag(NoexceptExpr->getExprLoc(),
39        "noexcept specifier on the move %select{assignment "
40        "operator|constructor}0 evaluates to 'false'")
41       << CXXConstructorDecl::classof(FuncDecl);
42 }
43 
44 } // namespace clang::tidy::performance
45