//===----------------------------------------------------------------------===//
#include "UnusedReturnValueCheck.h"
+#include "../utils/Matchers.h"
#include "../utils/OptionsUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
return InnerMatcher.matches(InstantiatedFrom ? *InstantiatedFrom : Node,
Finder, Builder);
}
-
} // namespace
UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
"::sigismember;"
"::strcasecmp;"
"::strsignal;"
- "::ttyname")) {}
+ "::ttyname")),
+ CheckedReturnTypes(utils::options::parseStringList(
+ Options.get("CheckedReturnTypes", "::std::error_code;"
+ "::std::expected;"
+ "::boost::system::error_code;"
+ "::abseil::Status"))) {}
void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "CheckedFunctions", CheckedFunctions);
+ Options.store(Opts, "CheckedReturnTypes",
+ utils::options::serializeStringList(CheckedReturnTypes));
}
void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
auto FunVec = utils::options::parseStringList(CheckedFunctions);
+
auto MatchedCallExpr = expr(ignoringImplicit(ignoringParenImpCasts(
callExpr(callee(functionDecl(
// Don't match void overloads of checked functions.
unless(returns(voidType())),
- isInstantiatedFrom(hasAnyName(FunVec)))))
+ anyOf(isInstantiatedFrom(hasAnyName(FunVec)),
+ returns(hasCanonicalType(hasDeclaration(
+ namedDecl(matchers::matchesAnyListedName(
+ CheckedReturnTypes)))))))))
.bind("match"))));
auto UnusedInCompoundStmt =
constructor initializers. Correctly handle constructor arguments as being
sequenced when constructor call is written as list-initialization.
+- Extend :doc:`bugprone-unused-return-value
+ <clang-tidy/checks/bugprone/unused-return-value>` check to check for all functions
+ with specified return types using the ``CheckedReturnTypes`` option.
+
- Deprecated :doc:`cert-dcl21-cpp
<clang-tidy/checks/cert/dcl21-cpp>` check.
return value often indicates that the programmer confused the function with
``clear()``.
+.. option:: CheckedReturnTypes
+
+ Semicolon-separated list of function return types to check.
+ By default the following function return types are checked:
+ `::std::error_code`, `::std::expected`, `::boost::system::error_code`, `::abseil::Status`
+
`cert-err33-c <../cert/err33-c.html>`_ is an alias of this check that checks a
fixed and large set of standard library functions.
bool empty() const noexcept;
};
+class error_code {
+};
+
// the check should be able to match std lib calls even if the functions are
// declared inside inline namespaces
inline namespace v1 {
void useFuture(const std::future &fut);
+std::error_code errorFunc() {
+ return std::error_code();
+}
+
void warning() {
std::async(increment, 42);
// CHECK-NOTES: [[@LINE-1]]:3: warning: the value returned by this function should be used
// CHECK-NOTES: [[@LINE-1]]:5: warning: the value {{.*}} should be used
// CHECK-NOTES: [[@LINE-2]]:5: note: cast {{.*}} this warning
}
+
+ errorFunc();
+ // CHECK-NOTES: [[@LINE-1]]:3: warning: the value {{.*}} should be used
+ // CHECK-NOTES: [[@LINE-2]]:3: note: cast {{.*}} this warning
}
void noWarning() {
std::vector<Foo> VecNoWarning;
auto VecEmptyRetval = VecNoWarning.empty();
+ (void) errorFunc();
+
// test using the return value in different kinds of expressions
useFuture(std::async(increment, 42));
std::launder(&FNoWarning)->f();