10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 #include "clang/Tooling/FixIt.h" 18 namespace readability {
20 void ElseAfterReturnCheck::registerMatchers(MatchFinder *Finder) {
21 const auto InterruptsControlFlow =
22 stmt(anyOf(returnStmt().bind(
"return"), continueStmt().bind(
"continue"),
23 breakStmt().bind(
"break"),
24 expr(ignoringImplicit(cxxThrowExpr().bind(
"throw")))));
27 ifStmt(unless(isConstexpr()),
34 unless(hasConditionVariableStatement(anything())),
35 hasThen(stmt(anyOf(InterruptsControlFlow,
36 compoundStmt(has(InterruptsControlFlow))))),
37 hasElse(stmt().bind(
"else")))
42 void ElseAfterReturnCheck::check(
const MatchFinder::MatchResult &
Result) {
43 const auto *If = Result.Nodes.getNodeAs<IfStmt>(
"if");
44 SourceLocation ElseLoc = If->getElseLoc();
45 std::string ControlFlowInterruptor;
46 for (
const auto *BindingName : {
"return",
"continue",
"break",
"throw"})
47 if (Result.Nodes.getNodeAs<Stmt>(BindingName))
48 ControlFlowInterruptor = BindingName;
50 DiagnosticBuilder Diag = diag(ElseLoc,
"do not use 'else' after '%0'")
51 << ControlFlowInterruptor;
52 Diag << tooling::fixit::createRemoval(ElseLoc);
56 if (
const auto *CS = Result.Nodes.getNodeAs<CompoundStmt>(
"else"))
57 Diag << tooling::fixit::createRemoval(CS->getLBracLoc())
58 << tooling::fixit::createRemoval(CS->getRBracLoc());
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result