11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Tooling/FixIt.h"
15 using namespace clang::ast_matchers;
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"), cxxThrowExpr().bind(
"throw")));
28 anyOf(ControlFlowInterruptorMatcher,
29 compoundStmt(has(ControlFlowInterruptorMatcher))))),
30 hasElse(stmt().bind(
"else")))
35 void ElseAfterReturnCheck::check(
const MatchFinder::MatchResult &
Result) {
36 const auto *If = Result.Nodes.getNodeAs<IfStmt>(
"if");
37 SourceLocation ElseLoc = If->getElseLoc();
38 std::string ControlFlowInterruptor;
39 for (
const auto *BindingName : {
"return",
"continue",
"break",
"throw"})
40 if (Result.Nodes.getNodeAs<Stmt>(BindingName))
41 ControlFlowInterruptor = BindingName;
43 DiagnosticBuilder Diag = diag(ElseLoc,
"do not use 'else' after '%0'")
44 << ControlFlowInterruptor;
45 Diag << tooling::fixit::createRemoval(ElseLoc);
49 if (
const auto *CS = Result.Nodes.getNodeAs<CompoundStmt>(
"else"))
50 Diag << tooling::fixit::createRemoval(CS->getLBracLoc())
51 << tooling::fixit::createRemoval(CS->getRBracLoc());
std::unique_ptr< ast_matchers::MatchFinder > Finder