10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/Lex/Lexer.h"
18 "redundant return statement at the end "
19 "of a function with a void return type";
21 "redundant continue statement at the "
22 "end of loop statement";
26 return SM.isMacroBodyExpansion(Loc) || SM.isMacroArgExpansion(Loc);
31 functionDecl(isDefinition(), returns(voidType()),
32 hasBody(compoundStmt(hasAnySubstatement(
33 returnStmt(unless(has(expr())))))
37 mapAnyOf(forStmt, cxxForRangeStmt, whileStmt, doStmt)
38 .with(hasBody(compoundStmt(hasAnySubstatement(continueStmt()))
44 if (
const auto *Return = Result.Nodes.getNodeAs<CompoundStmt>(
"return"))
45 checkRedundantReturn(Result, Return);
46 else if (
const auto *Continue =
47 Result.Nodes.getNodeAs<CompoundStmt>(
"continue"))
48 checkRedundantContinue(Result, Continue);
51void RedundantControlFlowCheck::checkRedundantReturn(
52 const MatchFinder::MatchResult &Result,
const CompoundStmt *
Block) {
53 CompoundStmt::const_reverse_body_iterator Last =
Block->body_rbegin();
54 if (
const auto *Return = dyn_cast<ReturnStmt>(*Last))
55 issueDiagnostic(Result,
Block, Return->getSourceRange(),
59void RedundantControlFlowCheck::checkRedundantContinue(
60 const MatchFinder::MatchResult &Result,
const CompoundStmt *
Block) {
61 CompoundStmt::const_reverse_body_iterator Last =
Block->body_rbegin();
62 if (
const auto *Continue = dyn_cast<ContinueStmt>(*Last))
63 issueDiagnostic(Result,
Block, Continue->getSourceRange(),
67void RedundantControlFlowCheck::issueDiagnostic(
68 const MatchFinder::MatchResult &Result,
const CompoundStmt *
const Block,
69 const SourceRange &StmtRange,
const char *
const Diag) {
70 SourceManager &SM = *Result.SourceManager;
74 CompoundStmt::const_reverse_body_iterator Previous = ++
Block->body_rbegin();
76 if (Previous !=
Block->body_rend())
77 Start = Lexer::findLocationAfterToken(
78 cast<Stmt>(*Previous)->getEndLoc(), tok::semi, SM, getLangOpts(),
81 Start = StmtRange.getBegin();
82 auto RemovedRange = CharSourceRange::getCharRange(
83 Start, Lexer::findLocationAfterToken(
84 StmtRange.getEnd(), tok::semi, SM, getLangOpts(),
87 diag(StmtRange.getBegin(), Diag) << FixItHint::CreateRemoval(RemovedRange);
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static const char *const RedundantContinueDiag
static bool isLocationInMacroExpansion(const SourceManager &SM, SourceLocation Loc)
static const char *const RedundantReturnDiag