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) {
25 forEach(ifStmt(hasThen(stmt(anyOf(returnStmt(),
26 compoundStmt(has(returnStmt()))))),
27 hasElse(stmt().bind(
"else")))
32 void ElseAfterReturnCheck::check(
const MatchFinder::MatchResult &
Result) {
33 const auto *If = Result.Nodes.getNodeAs<IfStmt>(
"if");
34 SourceLocation ElseLoc = If->getElseLoc();
35 DiagnosticBuilder Diag = diag(ElseLoc,
"don't use else after return");
36 Diag << tooling::fixit::createRemoval(ElseLoc);
40 if (
const auto *CS = Result.Nodes.getNodeAs<CompoundStmt>(
"else"))
41 Diag << tooling::fixit::createRemoval(CS->getLBracLoc())
42 << tooling::fixit::createRemoval(CS->getRBracLoc());
std::unique_ptr< ast_matchers::MatchFinder > Finder