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