1//===--- RedundantControlFlowCheck.cpp - clang-tidy------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "RedundantControlFlowCheck.h"
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/Lex/Lexer.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::readability {
16
17namespace {
18
19const char *const RedundantReturnDiag = "redundant return statement at the end "
20 "of a function with a void return type";
21const char *const RedundantContinueDiag = "redundant continue statement at the "
22 "end of loop statement";
23
24bool isLocationInMacroExpansion(const SourceManager &SM, SourceLocation Loc) {
25 return SM.isMacroBodyExpansion(Loc) || SM.isMacroArgExpansion(Loc);
26}
27
28} // namespace
29
30void RedundantControlFlowCheck::registerMatchers(MatchFinder *Finder) {
31 Finder->addMatcher(
32 NodeMatch: functionDecl(isDefinition(), returns(InnerMatcher: voidType()),
33 hasBody(InnerMatcher: compoundStmt(hasAnySubstatement(
34 InnerMatcher: returnStmt(unless(has(expr())))))
35 .bind(ID: "return"))),
36 Action: this);
37 Finder->addMatcher(
38 NodeMatch: mapAnyOf(forStmt, cxxForRangeStmt, whileStmt, doStmt)
39 .with(hasBody(InnerMatcher: compoundStmt(hasAnySubstatement(InnerMatcher: continueStmt()))
40 .bind(ID: "continue"))),
41 Action: this);
42}
43
44void RedundantControlFlowCheck::check(const MatchFinder::MatchResult &Result) {
45 if (const auto *Return = Result.Nodes.getNodeAs<CompoundStmt>(ID: "return"))
46 checkRedundantReturn(Result, Block: Return);
47 else if (const auto *Continue =
48 Result.Nodes.getNodeAs<CompoundStmt>(ID: "continue"))
49 checkRedundantContinue(Result, Block: Continue);
50}
51
52void RedundantControlFlowCheck::checkRedundantReturn(
53 const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
54 CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
55 if (const auto *Return = dyn_cast<ReturnStmt>(Val: *Last))
56 issueDiagnostic(Result, Block, StmtRange: Return->getSourceRange(),
57 Diag: RedundantReturnDiag);
58}
59
60void RedundantControlFlowCheck::checkRedundantContinue(
61 const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
62 CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
63 if (const auto *Continue = dyn_cast<ContinueStmt>(Val: *Last))
64 issueDiagnostic(Result, Block, StmtRange: Continue->getSourceRange(),
65 Diag: RedundantContinueDiag);
66}
67
68void RedundantControlFlowCheck::issueDiagnostic(
69 const MatchFinder::MatchResult &Result, const CompoundStmt *const Block,
70 const SourceRange &StmtRange, const char *const Diag) {
71 SourceManager &SM = *Result.SourceManager;
72 if (isLocationInMacroExpansion(SM, Loc: StmtRange.getBegin()))
73 return;
74
75 CompoundStmt::const_reverse_body_iterator Previous = ++Block->body_rbegin();
76 SourceLocation Start;
77 if (Previous != Block->body_rend())
78 Start = Lexer::findLocationAfterToken(
79 loc: cast<Stmt>(Val: *Previous)->getEndLoc(), TKind: tok::semi, SM, LangOpts: getLangOpts(),
80 /*SkipTrailingWhitespaceAndNewLine=*/true);
81 if (!Start.isValid())
82 Start = StmtRange.getBegin();
83 auto RemovedRange = CharSourceRange::getCharRange(
84 B: Start, E: Lexer::findLocationAfterToken(
85 loc: StmtRange.getEnd(), TKind: tok::semi, SM, LangOpts: getLangOpts(),
86 /*SkipTrailingWhitespaceAndNewLine=*/true));
87
88 diag(Loc: StmtRange.getBegin(), Description: Diag) << FixItHint::CreateRemoval(RemoveRange: RemovedRange);
89}
90
91} // namespace clang::tidy::readability
92

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of clang-tools-extra/clang-tidy/readability/RedundantControlFlowCheck.cpp