clang-tools 10.0.0

UnusedReturnValueCheck.cpp
Go to the documentation of this file.
1 //===--- UnusedReturnValueCheck.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 "UnusedReturnValueCheck.h"
10 #include "../utils/OptionsUtils.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13
14 using namespace clang::ast_matchers;
15 using namespace clang::ast_matchers::internal;
16
17 namespace clang {
18 namespace tidy {
19 namespace bugprone {
20
21 namespace {
22
23 // Matches functions that are instantiated from a class template member function
24 // matching InnerMatcher. Functions not instantiated from a class template
25 // member function are matched directly with InnerMatcher.
26 AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, Matcher<FunctionDecl>,
27 InnerMatcher) {
28 FunctionDecl *InstantiatedFrom = Node.getInstantiatedFromMemberFunction();
29 return InnerMatcher.matches(InstantiatedFrom ? *InstantiatedFrom : Node,
30 Finder, Builder);
31 }
32
33 } // namespace
34
35 UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
36 ClangTidyContext *Context)
37 : ClangTidyCheck(Name, Context),
38 CheckedFunctions(Options.get("CheckedFunctions",
39 "::std::async;"
40 "::std::launder;"
41 "::std::remove;"
42 "::std::remove_if;"
43 "::std::unique;"
44 "::std::unique_ptr::release;"
45 "::std::basic_string::empty;"
46 "::std::vector::empty")) {}
47
48 void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
49 Options.store(Opts, "CheckedFunctions", CheckedFunctions);
50 }
51
52 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
53 auto FunVec = utils::options::parseStringList(CheckedFunctions);
54 auto MatchedCallExpr = expr(ignoringImplicit(ignoringParenImpCasts(
55 callExpr(callee(functionDecl(
56 // Don't match void overloads of checked functions.
57 unless(returns(voidType())),
58 isInstantiatedFrom(hasAnyName(
59 std::vector<StringRef>(FunVec.begin(), FunVec.end()))))))
60 .bind("match"))));
61
62 auto UnusedInCompoundStmt =
63 compoundStmt(forEach(MatchedCallExpr),
64 // The checker can't currently differentiate between the
65 // return statement and other statements inside GNU statement
66 // expressions, so disable the checker inside them to avoid
67 // false positives.
68 unless(hasParent(stmtExpr())));
69 auto UnusedInIfStmt =
70 ifStmt(eachOf(hasThen(MatchedCallExpr), hasElse(MatchedCallExpr)));
71 auto UnusedInWhileStmt = whileStmt(hasBody(MatchedCallExpr));
72 auto UnusedInDoStmt = doStmt(hasBody(MatchedCallExpr));
73 auto UnusedInForStmt =
74 forStmt(eachOf(hasLoopInit(MatchedCallExpr),
75 hasIncrement(MatchedCallExpr), hasBody(MatchedCallExpr)));
76 auto UnusedInRangeForStmt = cxxForRangeStmt(hasBody(MatchedCallExpr));
77 auto UnusedInCaseStmt = switchCase(forEach(MatchedCallExpr));
78
79 Finder->addMatcher(
80 stmt(anyOf(UnusedInCompoundStmt, UnusedInIfStmt, UnusedInWhileStmt,
81 UnusedInDoStmt, UnusedInForStmt, UnusedInRangeForStmt,
82 UnusedInCaseStmt)),
83 this);
84 }
85
86 void UnusedReturnValueCheck::check(const MatchFinder::MatchResult &Result) {
87 if (const auto *Matched = Result.Nodes.getNodeAs<CallExpr>("match")) {
88 diag(Matched->getBeginLoc(),
89 "the value returned by this function should be used")
90 << Matched->getSourceRange();
91 diag(Matched->getBeginLoc(),
92 "cast the expression to void to silence this warning",
93 DiagnosticIDs::Note);
94 }
95 }
96
97 } // namespace bugprone
98 } // namespace tidy
99 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::utils::options::parseStringList
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
Definition: OptionsUtils.cpp:18
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
clang::tidy::bugprone::UnusedReturnValueCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: UnusedReturnValueCheck.cpp:86
clang::tidy::ClangTidyCheck::OptionsView::store
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.
Definition: ClangTidyCheck.cpp:58
clang::tidy::bugprone::UnusedReturnValueCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UnusedReturnValueCheck.cpp:52
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:35
clang::tidy::bugprone::UnusedReturnValueCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: UnusedReturnValueCheck.cpp:48
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
internal
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
UnusedReturnValueCheck.h
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21
clang::tidy::readability::AST_MATCHER_P
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)
Definition: ConvertMemberFunctionsToStatic.cpp:52