File indexing completed on 2024-05-12 05:41:01
0001 /* 0002 SPDX-FileCopyrightText: 2015 Sergio Martins <smartins@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "isempty-vs-count.h" 0008 #include "QtUtils.h" 0009 #include "StringUtils.h" 0010 0011 #include <clang/AST/DeclCXX.h> 0012 #include <clang/AST/Expr.h> 0013 #include <clang/AST/ExprCXX.h> 0014 #include <clang/AST/OperationKinds.h> 0015 #include <clang/AST/Stmt.h> 0016 #include <clang/AST/StmtIterator.h> 0017 #include <clang/Basic/LLVM.h> 0018 #include <llvm/ADT/StringRef.h> 0019 #include <llvm/Support/Casting.h> 0020 0021 class ClazyContext; 0022 0023 using namespace clang; 0024 0025 IsEmptyVSCount::IsEmptyVSCount(const std::string &name, ClazyContext *context) 0026 : CheckBase(name, context, Option_CanIgnoreIncludes) 0027 { 0028 } 0029 0030 void IsEmptyVSCount::VisitStmt(clang::Stmt *stmt) 0031 { 0032 auto *cast = dyn_cast<ImplicitCastExpr>(stmt); 0033 if (!cast || cast->getCastKind() != clang::CK_IntegralToBoolean) { 0034 return; 0035 } 0036 0037 auto *memberCall = dyn_cast<CXXMemberCallExpr>(*(cast->child_begin())); 0038 CXXMethodDecl *method = memberCall ? memberCall->getMethodDecl() : nullptr; 0039 0040 if (!clazy::functionIsOneOf(method, {"size", "count", "length"})) { 0041 return; 0042 } 0043 0044 if (!clazy::classIsOneOf(method->getParent(), clazy::qtContainers())) { 0045 return; 0046 } 0047 0048 if (clazy::classIsOneOf(method->getParent(), {"QMultiHash", "QMultiMap"}) && memberCall->getNumArgs() == 2) { 0049 emitWarning(stmt->getBeginLoc(), "use contains() instead"); 0050 return; 0051 } 0052 0053 if (clazy::classIsOneOf(method->getParent(), {"QHash", "QMap", "QMultiHash", "QMultiMap"}) && memberCall->getNumArgs() == 1) { 0054 emitWarning(stmt->getBeginLoc(), "use contains() instead"); 0055 return; 0056 } 0057 0058 emitWarning(stmt->getBeginLoc(), "use isEmpty() instead"); 0059 }