blob: 003c05f865ad30aeecf29388cdcefe6a17e4fc33 [file] [log] [blame]
Aaron Ballmanc3975b72016-02-01 15:31:15 +00001//===--- RedundantControlFlowCheck.cpp - clang-tidy------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Aaron Ballmanc3975b72016-02-01 15:31:15 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "RedundantControlFlowCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18namespace readability {
19
20namespace {
21
22const char *const RedundantReturnDiag = "redundant return statement at the end "
23 "of a function with a void return type";
24const char *const RedundantContinueDiag = "redundant continue statement at the "
25 "end of loop statement";
26
27bool isLocationInMacroExpansion(const SourceManager &SM, SourceLocation Loc) {
28 return SM.isMacroBodyExpansion(Loc) || SM.isMacroArgExpansion(Loc);
29}
30
31} // namespace
32
33void RedundantControlFlowCheck::registerMatchers(MatchFinder *Finder) {
34 Finder->addMatcher(
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000035 functionDecl(
36 isDefinition(), returns(voidType()),
37 has(compoundStmt(hasAnySubstatement(returnStmt(unless(has(expr())))))
38 .bind("return"))),
Aaron Ballmanc3975b72016-02-01 15:31:15 +000039 this);
40 auto CompoundContinue =
41 has(compoundStmt(hasAnySubstatement(continueStmt())).bind("continue"));
42 Finder->addMatcher(
43 stmt(anyOf(forStmt(), cxxForRangeStmt(), whileStmt(), doStmt()),
44 CompoundContinue),
45 this);
46}
47
48void RedundantControlFlowCheck::check(const MatchFinder::MatchResult &Result) {
49 if (const auto *Return = Result.Nodes.getNodeAs<CompoundStmt>("return"))
50 checkRedundantReturn(Result, Return);
51 else if (const auto *Continue =
52 Result.Nodes.getNodeAs<CompoundStmt>("continue"))
53 checkRedundantContinue(Result, Continue);
54}
55
56void RedundantControlFlowCheck::checkRedundantReturn(
57 const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
58 CompoundStmt::const_reverse_body_iterator last = Block->body_rbegin();
59 if (const auto *Return = dyn_cast<ReturnStmt>(*last))
60 issueDiagnostic(Result, Block, Return->getSourceRange(),
61 RedundantReturnDiag);
62}
63
64void RedundantControlFlowCheck::checkRedundantContinue(
65 const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
66 CompoundStmt::const_reverse_body_iterator last = Block->body_rbegin();
67 if (const auto *Continue = dyn_cast<ContinueStmt>(*last))
68 issueDiagnostic(Result, Block, Continue->getSourceRange(),
69 RedundantContinueDiag);
70}
71
72void RedundantControlFlowCheck::issueDiagnostic(
73 const MatchFinder::MatchResult &Result, const CompoundStmt *const Block,
74 const SourceRange &StmtRange, const char *const Diag) {
75 SourceManager &SM = *Result.SourceManager;
76 if (isLocationInMacroExpansion(SM, StmtRange.getBegin()))
77 return;
78
79 CompoundStmt::const_reverse_body_iterator Previous = ++Block->body_rbegin();
80 SourceLocation Start;
81 if (Previous != Block->body_rend())
82 Start = Lexer::findLocationAfterToken(
Stephen Kellyc09197e2018-08-09 22:43:02 +000083 dyn_cast<Stmt>(*Previous)->getEndLoc(), tok::semi, SM, getLangOpts(),
Aaron Ballmanc3975b72016-02-01 15:31:15 +000084 /*SkipTrailingWhitespaceAndNewLine=*/true);
Kirill Bobyrevd7f2a352016-09-16 10:12:08 +000085 if (!Start.isValid())
Aaron Ballmanc3975b72016-02-01 15:31:15 +000086 Start = StmtRange.getBegin();
87 auto RemovedRange = CharSourceRange::getCharRange(
Gabor Horvathafad84c2016-09-24 02:13:45 +000088 Start, Lexer::findLocationAfterToken(
89 StmtRange.getEnd(), tok::semi, SM, getLangOpts(),
90 /*SkipTrailingWhitespaceAndNewLine=*/true));
Aaron Ballmanc3975b72016-02-01 15:31:15 +000091
92 diag(StmtRange.getBegin(), Diag) << FixItHint::CreateRemoval(RemovedRange);
93}
94
95} // namespace readability
96} // namespace tidy
97} // namespace clang