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")))));
29 anyOf(ControlFlowInterruptorMatcher,
30 compoundStmt(has(ControlFlowInterruptorMatcher))))),
31 hasElse(stmt().bind(
"else")))
36 void ElseAfterReturnCheck::check(
const MatchFinder::MatchResult &Result) {
37 const auto *If = Result.Nodes.getNodeAs<IfStmt>(
"if");
38 SourceLocation ElseLoc = If->getElseLoc();
39 std::string ControlFlowInterruptor;
40 for (
const auto *BindingName : {
"return",
"continue",
"break",
"throw"})
41 if (Result.Nodes.getNodeAs<Stmt>(BindingName))
42 ControlFlowInterruptor = BindingName;
44 DiagnosticBuilder Diag = diag(ElseLoc,
"do not use 'else' after '%0'")
45 << ControlFlowInterruptor;
46 Diag << tooling::fixit::createRemoval(ElseLoc);
50 if (
const auto *CS = Result.Nodes.getNodeAs<CompoundStmt>(
"else"))
51 Diag << tooling::fixit::createRemoval(CS->getLBracLoc())
52 << tooling::fixit::createRemoval(CS->getRBracLoc());
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//