clang-tools 20.0.0git
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
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::misc {
16
18 ast_matchers::MatchFinder *Finder) {
19 const auto HasGoodReturnType =
20 cxxMethodDecl(returns(hasCanonicalType(lValueReferenceType(pointee(
21 unless(isConstQualified()),
22 anyOf(autoType(), hasDeclaration(equalsBoundNode("class"))))))));
23
24 const auto IsSelf = qualType(hasCanonicalType(
25 anyOf(hasDeclaration(equalsBoundNode("class")),
26 referenceType(pointee(hasDeclaration(equalsBoundNode("class")))))));
27 const auto IsAssign =
28 cxxMethodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())),
29 hasName("operator="), ofClass(recordDecl().bind("class")))
30 .bind("method");
31 const auto IsSelfAssign =
32 cxxMethodDecl(IsAssign, hasParameter(0, parmVarDecl(hasType(IsSelf))))
33 .bind("method");
34
35 Finder->addMatcher(
36 cxxMethodDecl(IsAssign, unless(HasGoodReturnType)).bind("ReturnType"),
37 this);
38
39 const auto BadSelf = qualType(hasCanonicalType(referenceType(
40 anyOf(lValueReferenceType(pointee(unless(isConstQualified()))),
41 rValueReferenceType(pointee(isConstQualified()))))));
42
43 Finder->addMatcher(
44 cxxMethodDecl(IsSelfAssign,
45 hasParameter(0, parmVarDecl(hasType(BadSelf))))
46 .bind("ArgumentType"),
47 this);
48
49 Finder->addMatcher(
50 cxxMethodDecl(IsSelfAssign, anyOf(isConst(), isVirtual())).bind("cv"),
51 this);
52
53 const auto IsBadReturnStatement = returnStmt(unless(has(ignoringParenImpCasts(
54 anyOf(unaryOperator(hasOperatorName("*"), hasUnaryOperand(cxxThisExpr())),
55 cxxOperatorCallExpr(argumentCountIs(1),
56 callee(unresolvedLookupExpr()),
57 hasArgument(0, cxxThisExpr())),
58 cxxOperatorCallExpr(
59 hasOverloadedOperatorName("="),
60 hasArgument(
61 0, unaryOperator(hasOperatorName("*"),
62 hasUnaryOperand(cxxThisExpr())))))))));
63 const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType);
64
65 Finder->addMatcher(returnStmt(IsBadReturnStatement, forFunction(IsGoodAssign))
66 .bind("returnStmt"),
67 this);
68}
69
71 const MatchFinder::MatchResult &Result) {
72 if (const auto *RetStmt = Result.Nodes.getNodeAs<ReturnStmt>("returnStmt")) {
73 diag(RetStmt->getBeginLoc(), "operator=() should always return '*this'");
74 } else {
75 const auto *Method = Result.Nodes.getNodeAs<CXXMethodDecl>("method");
76 if (Result.Nodes.getNodeAs<CXXMethodDecl>("ReturnType"))
77 diag(Method->getBeginLoc(), "operator=() should return '%0&'")
78 << Method->getParent()->getName();
79 if (Result.Nodes.getNodeAs<CXXMethodDecl>("ArgumentType"))
80 diag(Method->getBeginLoc(),
81 "operator=() should take '%0 const&'%select{|, '%0&&'}1 or '%0'")
82 << Method->getParent()->getName() << getLangOpts().CPlusPlus11;
83 if (Result.Nodes.getNodeAs<CXXMethodDecl>("cv"))
84 diag(Method->getBeginLoc(),
85 "operator=() should not be marked '%select{const|virtual}0'")
86 << !Method->isConst();
87 }
88}
89
90} // namespace clang::tidy::misc
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.