xref: /llvm-project/clang-tools-extra/clang-tidy/readability/SimplifySubscriptExprCheck.cpp (revision 12cb540529e41d12cf27d2e716a384b6692563a8)
1 //===--- SimplifySubscriptExprCheck.cpp - clang-tidy-----------------------===//
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 
9 #include "SimplifySubscriptExprCheck.h"
10 #include "../utils/OptionsUtils.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace readability {
19 
20 static const char KDefaultTypes[] =
21     "::std::basic_string;::std::basic_string_view;::std::vector;::std::array";
22 
23 SimplifySubscriptExprCheck::SimplifySubscriptExprCheck(
24     StringRef Name, ClangTidyContext *Context)
25     : ClangTidyCheck(Name, Context), Types(utils::options::parseStringList(
26                                          Options.get("Types", KDefaultTypes))) {
27 }
28 
29 void SimplifySubscriptExprCheck::registerMatchers(MatchFinder *Finder) {
30   const auto TypesMatcher = hasUnqualifiedDesugaredType(
31       recordType(hasDeclaration(cxxRecordDecl(hasAnyName(Types)))));
32 
33   Finder->addMatcher(
34       arraySubscriptExpr(hasBase(
35           cxxMemberCallExpr(
36               has(memberExpr().bind("member")),
37               on(hasType(qualType(
38                   unless(anyOf(substTemplateTypeParmType(),
39                                hasDescendant(substTemplateTypeParmType()))),
40                   anyOf(TypesMatcher, pointerType(pointee(TypesMatcher)))))),
41               callee(namedDecl(hasName("data"))))
42               .bind("call"))),
43       this);
44 }
45 
46 void SimplifySubscriptExprCheck::check(const MatchFinder::MatchResult &Result) {
47   const auto *Call = Result.Nodes.getNodeAs<CXXMemberCallExpr>("call");
48   if (Result.Context->getSourceManager().isMacroBodyExpansion(
49           Call->getExprLoc()))
50     return;
51 
52   const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
53   auto DiagBuilder =
54       diag(Member->getMemberLoc(),
55            "accessing an element of the container does not require a call to "
56            "'data()'; did you mean to use 'operator[]'?");
57   if (Member->isArrow())
58     DiagBuilder << FixItHint::CreateInsertion(Member->getBeginLoc(), "(*")
59                 << FixItHint::CreateInsertion(Member->getOperatorLoc(), ")");
60   DiagBuilder << FixItHint::CreateRemoval(
61       {Member->getOperatorLoc(), Call->getEndLoc()});
62 }
63 
64 void SimplifySubscriptExprCheck::storeOptions(
65     ClangTidyOptions::OptionMap &Opts) {
66   Options.store(Opts, "Types", utils::options::serializeStringList(Types));
67 }
68 
69 } // namespace readability
70 } // namespace tidy
71 } // namespace clang
72