clang-tools 22.0.0git
OverloadedUnaryAndCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
16
18 ast_matchers::MatchFinder *Finder) {
19 // Match unary methods that overload operator&.
20 Finder->addMatcher(
21 cxxMethodDecl(parameterCountIs(0), hasOverloadedOperatorName("&"))
22 .bind("overload"),
23 this);
24 // Also match freestanding unary operator& overloads. Be careful not to match
25 // binary methods.
26 Finder->addMatcher(functionDecl(unless(cxxMethodDecl()), parameterCountIs(1),
27 hasOverloadedOperatorName("&"))
28 .bind("overload"),
29 this);
30}
31
32void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult &Result) {
33 const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("overload");
34 diag(Decl->getBeginLoc(),
35 "do not overload unary operator&, it is dangerous.");
36}
37
38} // namespace clang::tidy::google::runtime
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override