xref: /llvm-project/clang-tools-extra/clang-tidy/readability/MisplacedArrayIndexCheck.cpp (revision 7d2ea6c422d3f5712b7253407005e1a465a76946)
1 //===--- MisplacedArrayIndexCheck.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 "MisplacedArrayIndexCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13 #include "clang/Tooling/FixIt.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang::tidy::readability {
18 
registerMatchers(MatchFinder * Finder)19 void MisplacedArrayIndexCheck::registerMatchers(MatchFinder *Finder) {
20   Finder->addMatcher(
21       traverse(TK_AsIs, arraySubscriptExpr(hasLHS(hasType(isInteger())),
22                                            hasRHS(hasType(isAnyPointer())))
23                             .bind("expr")),
24       this);
25 }
26 
check(const MatchFinder::MatchResult & Result)27 void MisplacedArrayIndexCheck::check(const MatchFinder::MatchResult &Result) {
28   const auto *ArraySubscriptE =
29       Result.Nodes.getNodeAs<ArraySubscriptExpr>("expr");
30 
31   auto Diag = diag(ArraySubscriptE->getBeginLoc(), "confusing array subscript "
32                                                    "expression, usually the "
33                                                    "index is inside the []");
34 
35   // Only try to fixit when LHS and RHS can be swapped directly without changing
36   // the logic.
37   const Expr *RHSE = ArraySubscriptE->getRHS()->IgnoreParenImpCasts();
38   if (!isa<StringLiteral>(RHSE) && !isa<DeclRefExpr>(RHSE) &&
39       !isa<MemberExpr>(RHSE))
40     return;
41 
42   const StringRef LText = tooling::fixit::getText(
43       ArraySubscriptE->getLHS()->getSourceRange(), *Result.Context);
44   const StringRef RText = tooling::fixit::getText(
45       ArraySubscriptE->getRHS()->getSourceRange(), *Result.Context);
46 
47   Diag << FixItHint::CreateReplacement(
48       ArraySubscriptE->getLHS()->getSourceRange(), RText);
49   Diag << FixItHint::CreateReplacement(
50       ArraySubscriptE->getRHS()->getSourceRange(), LText);
51 }
52 
53 } // namespace clang::tidy::readability
54