10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
20const char *
const RedundantReturnDiag =
"redundant return statement at the end "
21 "of a function with a void return type";
22const char *
const RedundantContinueDiag =
"redundant continue statement at the "
23 "end of loop statement";
25bool isLocationInMacroExpansion(
const SourceManager &SM, SourceLocation
Loc) {
26 return SM.isMacroBodyExpansion(
Loc) || SM.isMacroArgExpansion(
Loc);
33 functionDecl(isDefinition(), returns(voidType()),
34 hasBody(compoundStmt(hasAnySubstatement(
35 returnStmt(unless(has(expr())))))
39 mapAnyOf(forStmt, cxxForRangeStmt, whileStmt, doStmt)
40 .with(hasBody(compoundStmt(hasAnySubstatement(continueStmt()))
46 if (
const auto *Return = Result.Nodes.getNodeAs<CompoundStmt>(
"return"))
47 checkRedundantReturn(Result, Return);
48 else if (
const auto *Continue =
49 Result.Nodes.getNodeAs<CompoundStmt>(
"continue"))
50 checkRedundantContinue(Result, Continue);
53void RedundantControlFlowCheck::checkRedundantReturn(
54 const MatchFinder::MatchResult &Result,
const CompoundStmt *Block) {
55 CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
56 if (
const auto *Return = dyn_cast<ReturnStmt>(*Last))
57 issueDiagnostic(Result, Block, Return->getSourceRange(),
61void RedundantControlFlowCheck::checkRedundantContinue(
62 const MatchFinder::MatchResult &Result,
const CompoundStmt *Block) {
63 CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
64 if (
const auto *Continue = dyn_cast<ContinueStmt>(*Last))
65 issueDiagnostic(Result, Block, Continue->getSourceRange(),
66 RedundantContinueDiag);
69void RedundantControlFlowCheck::issueDiagnostic(
70 const MatchFinder::MatchResult &Result,
const CompoundStmt *
const Block,
71 const SourceRange &StmtRange,
const char *
const Diag) {
72 SourceManager &SM = *Result.SourceManager;
73 if (isLocationInMacroExpansion(SM, StmtRange.getBegin()))
76 CompoundStmt::const_reverse_body_iterator Previous = ++Block->body_rbegin();
78 if (Previous != Block->body_rend())
79 Start = Lexer::findLocationAfterToken(
80 cast<Stmt>(*Previous)->getEndLoc(), tok::semi, SM,
getLangOpts(),
83 Start = StmtRange.getBegin();
84 auto RemovedRange = CharSourceRange::getCharRange(
85 Start, Lexer::findLocationAfterToken(
89 diag(StmtRange.getBegin(), Diag) << FixItHint::CreateRemoval(RemovedRange);
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.