clang-tools 19.0.0git
AvoidReturnWithVoidValueCheck.cpp
Go to the documentation of this file.
1//===--- AvoidReturnWithVoidValueCheck.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
10#include "../utils/BracesAroundStatement.h"
11#include "../utils/LexerUtils.h"
12
13using namespace clang::ast_matchers;
14
16
17static constexpr char IgnoreMacrosName[] = "IgnoreMacros";
18static const bool IgnoreMacrosDefault = true;
19
20static constexpr char StrictModeName[] = "StrictMode";
21static const bool StrictModeDefault = true;
22
24 StringRef Name, ClangTidyContext *Context)
25 : ClangTidyCheck(Name, Context),
26 IgnoreMacros(
27 Options.getLocalOrGlobal(IgnoreMacrosName, IgnoreMacrosDefault)),
28 StrictMode(Options.getLocalOrGlobal(StrictModeName, StrictModeDefault)) {}
29
31 Finder->addMatcher(
32 returnStmt(
33 hasReturnValue(allOf(hasType(voidType()), unless(initListExpr()))),
34 optionally(hasParent(
35 compoundStmt(
36 optionally(hasParent(functionDecl().bind("function_parent"))))
37 .bind("compound_parent"))))
38 .bind("void_return"),
39 this);
40}
41
43 const MatchFinder::MatchResult &Result) {
44 const auto *VoidReturn = Result.Nodes.getNodeAs<ReturnStmt>("void_return");
45 if (IgnoreMacros && VoidReturn->getBeginLoc().isMacroID())
46 return;
47 const auto *SurroundingBlock =
48 Result.Nodes.getNodeAs<CompoundStmt>("compound_parent");
49 if (!StrictMode && !SurroundingBlock)
50 return;
51 DiagnosticBuilder Diag = diag(VoidReturn->getBeginLoc(),
52 "return statement within a void function "
53 "should not have a specified return value");
54 const SourceLocation SemicolonPos = utils::lexer::findNextTerminator(
55 VoidReturn->getEndLoc(), *Result.SourceManager, getLangOpts());
56 if (SemicolonPos.isInvalid())
57 return;
58 if (!SurroundingBlock) {
59 const auto BraceInsertionHints = utils::getBraceInsertionsHints(
60 VoidReturn, getLangOpts(), *Result.SourceManager,
61 VoidReturn->getBeginLoc());
62 if (BraceInsertionHints)
63 Diag << BraceInsertionHints.openingBraceFixIt()
64 << BraceInsertionHints.closingBraceFixIt();
65 }
66 Diag << FixItHint::CreateRemoval(VoidReturn->getReturnLoc());
67 const auto *FunctionParent =
68 Result.Nodes.getNodeAs<FunctionDecl>("function_parent");
69 if (!FunctionParent ||
70 (SurroundingBlock && SurroundingBlock->body_back() != VoidReturn))
71 // If this is not the last statement in a function body, we add a `return`.
72 Diag << FixItHint::CreateInsertion(SemicolonPos.getLocWithOffset(1),
73 " return;", true);
74}
75
76void AvoidReturnWithVoidValueCheck::storeOptions(
78 Options.store(Opts, IgnoreMacrosName, IgnoreMacros);
79 Options.store(Opts, StrictModeName, StrictMode);
80}
81
82} // namespace clang::tidy::readability
llvm::SmallString< 256U > Name
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
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.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
AvoidReturnWithVoidValueCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static constexpr char IgnoreMacrosName[]
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Definition: LexerUtils.cpp:82
BraceInsertionHints getBraceInsertionsHints(const Stmt *const S, const LangOptions &LangOpts, const SourceManager &SM, SourceLocation StartLoc, SourceLocation EndLocHint)
Create fix-it hints for braces that wrap the given statement when applied.
llvm::StringMap< ClangTidyValue > OptionMap