/* SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB a KDAB Group company info@kdab.com SPDX-FileContributor: Shivam Kunwar SPDX-License-Identifier: LGPL-2.0-or-later */ #include "unused-result-check.h" #include "StringUtils.h" #include #include #include #include #include using namespace clang::ast_matchers; using namespace clang; class Caller : public ClazyAstMatcherCallback { public: explicit Caller(CheckBase *check) : ClazyAstMatcherCallback(check) { } void run(const MatchFinder::MatchResult &result) override { if (const auto *callExpr = result.Nodes.getNodeAs("opCallExpr")) { if (const auto *methodDecl = callExpr->getMethodDecl(); methodDecl && methodDecl->isConst() && !methodDecl->getReturnType()->isVoidType()) { const auto &parents = result.Context->getParents(*callExpr); if (parents[0].get() != nullptr && parents[0].get() == nullptr) { if (!llvm::dyn_cast(parents[0].get()) && !llvm::dyn_cast(parents[0].get()) && !llvm::dyn_cast(parents[0].get()) && !llvm::dyn_cast(parents[0].get()) && !llvm::dyn_cast(parents[0].get()) && !llvm::dyn_cast(parents[0].get()) && !llvm::dyn_cast(parents[0].get()) && !llvm::dyn_cast(parents[0].get()) // Ignore this false positive from a Qt header for now, we pass a mutable pointer to a static function && clazy::qualifiedMethodName(callExpr->getMethodDecl()) != "QMetaType::registerHelper") { m_check->emitWarning(callExpr->getExprLoc(), "Result of const member function is not used."); } } else if (parents[0].get() != nullptr && parents[0].get() == nullptr) { if (!llvm::dyn_cast(parents[0].get()) && !llvm::dyn_cast(parents[0].get())) { m_check->emitWarning(callExpr->getExprLoc(), "Result of const member function is not used."); } } } } } }; UnusedResultCheck::~UnusedResultCheck() = default; void UnusedResultCheck::registerASTMatchers(MatchFinder &finder) { m_astMatcherCallBack = std::make_unique(this); finder.addMatcher(cxxMemberCallExpr().bind("opCallExpr"), m_astMatcherCallBack.get()); }