[clang-tools-extra] [clang-tidy] Improve bugprone-unused-return-value check (PR #66573)
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 25 12:41:22 PDT 2023
================
@@ -130,26 +130,35 @@ UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
"::std::error_condition;"
"::std::errc;"
"::std::expected;"
- "::boost::system::error_code"))) {}
+ "::boost::system::error_code"))),
+ AllowCastToVoid(Options.get("AllowCastToVoid", false)) {}
void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "CheckedFunctions", CheckedFunctions);
Options.store(Opts, "CheckedReturnTypes",
utils::options::serializeStringList(CheckedReturnTypes));
+ Options.store(Opts, "AllowCastToVoid", AllowCastToVoid);
}
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())),
- anyOf(isInstantiatedFrom(hasAnyName(FunVec)),
- returns(hasCanonicalType(hasDeclaration(
- namedDecl(matchers::matchesAnyListedName(
- CheckedReturnTypes)))))))))
- .bind("match"))));
+ auto MatchedDirectCallExpr =
+ expr(callExpr(callee(functionDecl(
+ // Don't match void overloads of checked functions.
+ unless(returns(voidType())),
+ anyOf(isInstantiatedFrom(hasAnyName(FunVec)),
+ returns(hasCanonicalType(hasDeclaration(
+ namedDecl(matchers::matchesAnyListedName(
+ CheckedReturnTypes)))))))))
+ .bind("match"));
+
+ auto CheckCastToVoid =
+ AllowCastToVoid ? castExpr(unless(hasCastKind(CK_ToVoid))) : castExpr();
+ auto MatchedCallExpr = expr(
+ anyOf(MatchedDirectCallExpr,
+ explicitCastExpr(unless(cxxFunctionalCastExpr()), CheckCastToVoid,
----------------
PiotrZSL wrote:
It actually does the same.. explicitCastExpr inherit from castExpr, so you can use parent matcher also.
https://github.com/llvm/llvm-project/pull/66573
More information about the cfe-commits
mailing list