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