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 9 #include "UnconventionalAssignOperatorCheck.h" 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 = 22 cxxMethodDecl(returns(hasCanonicalType(lValueReferenceType(pointee( 23 unless(isConstQualified()), 24 anyOf(autoType(), hasDeclaration(equalsBoundNode("class")))))))); 25 26 const auto IsSelf = qualType(hasCanonicalType( 27 anyOf(hasDeclaration(equalsBoundNode("class")), 28 referenceType(pointee(hasDeclaration(equalsBoundNode("class"))))))); 29 const auto IsAssign = 30 cxxMethodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())), 31 hasName("operator="), ofClass(recordDecl().bind("class"))) 32 .bind("method"); 33 const auto IsSelfAssign = 34 cxxMethodDecl(IsAssign, hasParameter(0, parmVarDecl(hasType(IsSelf)))) 35 .bind("method"); 36 37 Finder->addMatcher( 38 cxxMethodDecl(IsAssign, unless(HasGoodReturnType)).bind("ReturnType"), 39 this); 40 41 const auto BadSelf = qualType(hasCanonicalType(referenceType( 42 anyOf(lValueReferenceType(pointee(unless(isConstQualified()))), 43 rValueReferenceType(pointee(isConstQualified())))))); 44 45 Finder->addMatcher( 46 cxxMethodDecl(IsSelfAssign, 47 hasParameter(0, parmVarDecl(hasType(BadSelf)))) 48 .bind("ArgumentType"), 49 this); 50 51 Finder->addMatcher( 52 cxxMethodDecl(IsSelfAssign, anyOf(isConst(), isVirtual())).bind("cv"), 53 this); 54 55 const auto IsBadReturnStatement = returnStmt(unless(has(ignoringParenImpCasts( 56 anyOf(unaryOperator(hasOperatorName("*"), hasUnaryOperand(cxxThisExpr())), 57 cxxOperatorCallExpr(argumentCountIs(1), 58 callee(unresolvedLookupExpr()), 59 hasArgument(0, cxxThisExpr())), 60 cxxOperatorCallExpr( 61 hasOverloadedOperatorName("="), 62 hasArgument( 63 0, unaryOperator(hasOperatorName("*"), 64 hasUnaryOperand(cxxThisExpr()))))))))); 65 const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType); 66 67 Finder->addMatcher(returnStmt(IsBadReturnStatement, forFunction(IsGoodAssign)) 68 .bind("returnStmt"), 69 this); 70 } 71 72 void UnconventionalAssignOperatorCheck::check( 73 const MatchFinder::MatchResult &Result) { 74 if (const auto *RetStmt = Result.Nodes.getNodeAs<ReturnStmt>("returnStmt")) { 75 diag(RetStmt->getBeginLoc(), "operator=() should always return '*this'"); 76 } else { 77 const auto *Method = Result.Nodes.getNodeAs<CXXMethodDecl>("method"); 78 if (Result.Nodes.getNodeAs<CXXMethodDecl>("ReturnType")) 79 diag(Method->getBeginLoc(), "operator=() should return '%0&'") 80 << Method->getParent()->getName(); 81 if (Result.Nodes.getNodeAs<CXXMethodDecl>("ArgumentType")) 82 diag(Method->getBeginLoc(), 83 "operator=() should take '%0 const&'%select{|, '%0&&'}1 or '%0'") 84 << Method->getParent()->getName() << getLangOpts().CPlusPlus11; 85 if (Result.Nodes.getNodeAs<CXXMethodDecl>("cv")) 86 diag(Method->getBeginLoc(), 87 "operator=() should not be marked '%select{const|virtual}0'") 88 << !Method->isConst(); 89 } 90 } 91 92 } // namespace misc 93 } // namespace tidy 94 } // namespace clang 95