10#include "../utils/BracesAroundStatement.h"
11#include "../utils/LexerUtils.h"
33 hasReturnValue(allOf(hasType(voidType()), unless(initListExpr()))),
36 optionally(hasParent(functionDecl().bind(
"function_parent"))))
37 .bind(
"compound_parent"))))
43 const MatchFinder::MatchResult &Result) {
44 const auto *VoidReturn = Result.Nodes.getNodeAs<ReturnStmt>(
"void_return");
45 if (IgnoreMacros && VoidReturn->getBeginLoc().isMacroID())
47 const auto *SurroundingBlock =
48 Result.Nodes.getNodeAs<CompoundStmt>(
"compound_parent");
49 if (!StrictMode && !SurroundingBlock)
51 DiagnosticBuilder Diag =
diag(VoidReturn->getBeginLoc(),
52 "return statement within a void function "
53 "should not have a specified return value");
55 VoidReturn->getEndLoc(), *Result.SourceManager,
getLangOpts());
56 if (SemicolonPos.isInvalid())
58 if (!SurroundingBlock) {
61 VoidReturn->getBeginLoc());
62 if (BraceInsertionHints)
63 Diag << BraceInsertionHints.openingBraceFixIt()
64 << BraceInsertionHints.closingBraceFixIt();
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))
72 Diag << FixItHint::CreateInsertion(SemicolonPos.getLocWithOffset(1),
76void AvoidReturnWithVoidValueCheck::storeOptions(
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 const bool StrictModeDefault
static constexpr char StrictModeName[]
static const bool IgnoreMacrosDefault
static constexpr char IgnoreMacrosName[]
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
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