clang-tools  14.0.0git
TerminatingContinueCheck.cpp
Go to the documentation of this file.
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 
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 {
18 namespace tidy {
19 namespace bugprone {
20 
21 void TerminatingContinueCheck::registerMatchers(MatchFinder *Finder) {
22  const auto DoWithFalse =
23  doStmt(hasCondition(ignoringImpCasts(
24  anyOf(cxxBoolLiteral(equals(false)), integerLiteral(equals(0)),
25  cxxNullPtrLiteralExpr(), gnuNullExpr()))),
26  equalsBoundNode("closestLoop"));
27 
28  Finder->addMatcher(
29  continueStmt(
30  hasAncestor(stmt(anyOf(forStmt(), whileStmt(), cxxForRangeStmt(),
31  doStmt(), switchStmt()))
32  .bind("closestLoop")),
33  hasAncestor(DoWithFalse))
34  .bind("continue"),
35  this);
36 }
37 
38 void TerminatingContinueCheck::check(const MatchFinder::MatchResult &Result) {
39  const auto *ContStmt = Result.Nodes.getNodeAs<ContinueStmt>("continue");
40 
41  auto Diag =
42  diag(ContStmt->getBeginLoc(),
43  "'continue' in loop with false condition is equivalent to 'break'")
44  << tooling::fixit::createReplacement(*ContStmt, "break");
45 }
46 
47 } // namespace bugprone
48 } // namespace tidy
49 } // namespace clang
TerminatingContinueCheck.h
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::clangd::check
bool check(llvm::StringRef File, llvm::function_ref< bool(const Position &)> ShouldCheckLine, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:259
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27