UnconventionalAssignOperatorCheck.cpp 3.7 KB

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