clang-tools  11.0.0
UnconventionalAssignOperatorCheck.cpp
Go to the documentation of this file.
1 //===--- UnconventionalAssignOperatorCheck.cpp - 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 
10 #include "clang/ASTMatchers/ASTMatchFinder.h"
11 #include "clang/ASTMatchers/ASTMatchers.h"
12 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace misc {
18 
19 void UnconventionalAssignOperatorCheck::registerMatchers(
20  ast_matchers::MatchFinder *Finder) {
21  const auto HasGoodReturnType = cxxMethodDecl(returns(lValueReferenceType(
22  pointee(unless(isConstQualified()),
23  anyOf(autoType(), hasDeclaration(equalsBoundNode("class")))))));
24 
25  const auto IsSelf = qualType(
26  anyOf(hasDeclaration(equalsBoundNode("class")),
27  referenceType(pointee(hasDeclaration(equalsBoundNode("class"))))));
28  const auto IsAssign =
29  cxxMethodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())),
30  hasName("operator="), ofClass(recordDecl().bind("class")))
31  .bind("method");
32  const auto IsSelfAssign =
33  cxxMethodDecl(IsAssign, hasParameter(0, parmVarDecl(hasType(IsSelf))))
34  .bind("method");
35 
36  Finder->addMatcher(
37  cxxMethodDecl(IsAssign, unless(HasGoodReturnType)).bind("ReturnType"),
38  this);
39 
40  const auto BadSelf = referenceType(
41  anyOf(lValueReferenceType(pointee(unless(isConstQualified()))),
42  rValueReferenceType(pointee(isConstQualified()))));
43 
44  Finder->addMatcher(
45  cxxMethodDecl(IsSelfAssign,
46  hasParameter(0, parmVarDecl(hasType(BadSelf))))
47  .bind("ArgumentType"),
48  this);
49 
50  Finder->addMatcher(
51  cxxMethodDecl(IsSelfAssign, anyOf(isConst(), isVirtual())).bind("cv"),
52  this);
53 
54  const auto IsBadReturnStatement = returnStmt(unless(has(ignoringParenImpCasts(
55  anyOf(unaryOperator(hasOperatorName("*"), hasUnaryOperand(cxxThisExpr())),
56  cxxOperatorCallExpr(argumentCountIs(1),
57  callee(unresolvedLookupExpr()),
58  hasArgument(0, cxxThisExpr())),
59  cxxOperatorCallExpr(
60  hasOverloadedOperatorName("="),
61  hasArgument(
62  0, unaryOperator(hasOperatorName("*"),
63  hasUnaryOperand(cxxThisExpr())))))))));
64  const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType);
65 
66  Finder->addMatcher(returnStmt(IsBadReturnStatement, forFunction(IsGoodAssign))
67  .bind("returnStmt"),
68  this);
69 }
70 
71 void UnconventionalAssignOperatorCheck::check(
72  const MatchFinder::MatchResult &Result) {
73  if (const auto *RetStmt = Result.Nodes.getNodeAs<ReturnStmt>("returnStmt")) {
74  diag(RetStmt->getBeginLoc(), "operator=() should always return '*this'");
75  } else {
76  static const char *const Messages[][2] = {
77  {"ReturnType", "operator=() should return '%0&'"},
78  {"ArgumentType",
79  getLangOpts().CPlusPlus11
80  ? "operator=() should take '%0 const&', '%0&&' or '%0'"
81  : "operator=() should take '%0 const&' or '%0'"},
82  {"cv", "operator=() should not be marked '%1'"}};
83 
84  const auto *Method = Result.Nodes.getNodeAs<CXXMethodDecl>("method");
85  for (const auto &Message : Messages) {
86  if (Result.Nodes.getNodeAs<Decl>(Message[0]))
87  diag(Method->getBeginLoc(), Message[1])
88  << Method->getParent()->getName()
89  << (Method->isConst() ? "const" : "virtual");
90  }
91  }
92 }
93 
94 } // namespace misc
95 } // namespace tidy
96 } // namespace clang
clang::tidy::bugprone::Message
static const char Message[]
Definition: ReservedIdentifierCheck.cpp:31
clang::ast_matchers
Definition: AbseilMatcher.h:14
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
UnconventionalAssignOperatorCheck.h