11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Tooling/FixIt.h" 19 namespace readability {
21 void ElseAfterReturnCheck::registerMatchers(MatchFinder *Finder) {
22 const auto ControlFlowInterruptorMatcher =
23 stmt(anyOf(returnStmt().bind(
"return"), continueStmt().bind(
"continue"),
24 breakStmt().bind(
"break"),
25 expr(ignoringImplicit(cxxThrowExpr().bind(
"throw")))));
28 ifStmt(unless(isConstexpr()),
30 anyOf(ControlFlowInterruptorMatcher,
31 compoundStmt(has(ControlFlowInterruptorMatcher))))),
32 hasElse(stmt().bind(
"else")))
37 void ElseAfterReturnCheck::check(
const MatchFinder::MatchResult &
Result) {
38 const auto *If = Result.Nodes.getNodeAs<IfStmt>(
"if");
39 SourceLocation ElseLoc = If->getElseLoc();
40 std::string ControlFlowInterruptor;
41 for (
const auto *BindingName : {
"return",
"continue",
"break",
"throw"})
42 if (Result.Nodes.getNodeAs<Stmt>(BindingName))
43 ControlFlowInterruptor = BindingName;
45 DiagnosticBuilder Diag = diag(ElseLoc,
"do not use 'else' after '%0'")
46 << ControlFlowInterruptor;
47 Diag << tooling::fixit::createRemoval(ElseLoc);
51 if (
const auto *CS = Result.Nodes.getNodeAs<CompoundStmt>(
"else"))
52 Diag << tooling::fixit::createRemoval(CS->getLBracLoc())
53 << tooling::fixit::createRemoval(CS->getRBracLoc());
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//