| 1 | //===--- TerminatingContinueCheck.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 "TerminatingContinueCheck.h" |
| 10 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 11 | #include "clang/Lex/Lexer.h" |
| 12 | #include "clang/Tooling/FixIt.h" |
| 13 | |
| 14 | using namespace clang::ast_matchers; |
| 15 | |
| 16 | namespace clang::tidy::bugprone { |
| 17 | |
| 18 | void TerminatingContinueCheck::registerMatchers(MatchFinder *Finder) { |
| 19 | const auto DoWithFalse = |
| 20 | doStmt(hasCondition(InnerMatcher: ignoringImpCasts( |
| 21 | InnerMatcher: anyOf(cxxBoolLiteral(equals(Value: false)), integerLiteral(equals(Value: 0)), |
| 22 | cxxNullPtrLiteralExpr(), gnuNullExpr()))), |
| 23 | equalsBoundNode(ID: "closestLoop" )); |
| 24 | |
| 25 | Finder->addMatcher( |
| 26 | NodeMatch: continueStmt( |
| 27 | hasAncestor(stmt(anyOf(forStmt(), whileStmt(), cxxForRangeStmt(), |
| 28 | doStmt(), switchStmt())) |
| 29 | .bind(ID: "closestLoop" )), |
| 30 | hasAncestor(DoWithFalse)) |
| 31 | .bind(ID: "continue" ), |
| 32 | Action: this); |
| 33 | } |
| 34 | |
| 35 | void TerminatingContinueCheck::check(const MatchFinder::MatchResult &Result) { |
| 36 | const auto *ContStmt = Result.Nodes.getNodeAs<ContinueStmt>(ID: "continue" ); |
| 37 | |
| 38 | auto Diag = |
| 39 | diag(Loc: ContStmt->getBeginLoc(), |
| 40 | Description: "'continue' in loop with false condition is equivalent to 'break'" ) |
| 41 | << tooling::fixit::createReplacement(Destination: *ContStmt, Source: "break" ); |
| 42 | } |
| 43 | |
| 44 | } // namespace clang::tidy::bugprone |
| 45 | |