1 //===--- AvoidConstOrRefDataMembersCheck.cpp - clang-tidy -----------------===//
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 "AvoidConstOrRefDataMembersCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers
;
17 namespace cppcoreguidelines
{
20 AST_MATCHER(FieldDecl
, isMemberOfLambda
) {
21 return Node
.getParent()->isLambda();
26 void AvoidConstOrRefDataMembersCheck::registerMatchers(MatchFinder
*Finder
) {
27 Finder
->addMatcher(fieldDecl(unless(isMemberOfLambda()),
28 hasType(hasCanonicalType(referenceType())))
31 Finder
->addMatcher(fieldDecl(unless(isMemberOfLambda()),
32 hasType(qualType(isConstQualified())))
37 void AvoidConstOrRefDataMembersCheck::check(
38 const MatchFinder::MatchResult
&Result
) {
39 if (const auto *MatchedDecl
= Result
.Nodes
.getNodeAs
<FieldDecl
>("ref"))
40 diag(MatchedDecl
->getLocation(), "member %0 of type %1 is a reference")
41 << MatchedDecl
<< MatchedDecl
->getType();
42 if (const auto *MatchedDecl
= Result
.Nodes
.getNodeAs
<FieldDecl
>("const"))
43 diag(MatchedDecl
->getLocation(), "member %0 of type %1 is const qualified")
44 << MatchedDecl
<< MatchedDecl
->getType();
47 } // namespace cppcoreguidelines