1 //===--- OverloadedUnaryAndCheck.cpp - clang-tidy ---------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "OverloadedUnaryAndCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
14 using namespace clang::ast_matchers
;
16 namespace clang::tidy::google::runtime
{
18 void OverloadedUnaryAndCheck::registerMatchers(
19 ast_matchers::MatchFinder
*Finder
) {
20 // Match unary methods that overload operator&.
22 cxxMethodDecl(parameterCountIs(0), hasOverloadedOperatorName("&"))
25 // Also match freestanding unary operator& overloads. Be careful not to match
27 Finder
->addMatcher(functionDecl(unless(cxxMethodDecl()), parameterCountIs(1),
28 hasOverloadedOperatorName("&"))
33 void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult
&Result
) {
34 const auto *Decl
= Result
.Nodes
.getNodeAs
<FunctionDecl
>("overload");
35 diag(Decl
->getBeginLoc(),
36 "do not overload unary operator&, it is dangerous.");
39 } // namespace clang::tidy::google::runtime