/* SPDX-FileCopyrightText: 2015 Sergio Martins SPDX-FileCopyrightText: 2015 Mathias Hasselmann SPDX-License-Identifier: LGPL-2.0-or-later */ #include "auto-unexpected-qstringbuilder.h" #include "FixItUtils.h" #include "StringUtils.h" #include "TypeUtils.h" #include #include #include #include #include #include #include #include #include #include #include #include using namespace clang; static bool isQStringBuilder(QualType t) { const CXXRecordDecl *record = clazy::typeAsRecord(t); return record && clazy::name(record) == "QStringBuilder"; } void AutoUnexpectedQStringBuilder::VisitDecl(Decl *decl) { auto *varDecl = dyn_cast(decl); if (!varDecl) { return; } QualType qualtype = varDecl->getType(); const Type *type = qualtype.getTypePtrOrNull(); if (!type || !type->isRecordType() || !dyn_cast(type) || !isQStringBuilder(qualtype)) { return; } std::string replacement = "QString " + clazy::name(varDecl).str(); if (qualtype.isConstQualified()) { replacement = "const " + replacement; } SourceLocation start = varDecl->getBeginLoc(); SourceLocation end = varDecl->getLocation(); std::vector fixits; fixits.push_back(clazy::createReplacement({start, end}, replacement)); emitWarning(decl->getBeginLoc(), "auto deduced to be QStringBuilder instead of QString. Possible crash.", fixits); } void AutoUnexpectedQStringBuilder::VisitStmt(Stmt *stmt) { auto *lambda = dyn_cast(stmt); if (!lambda) { return; } CXXMethodDecl *method = lambda->getCallOperator(); if (!method || !isQStringBuilder(method->getReturnType())) { return; } emitWarning(stmt->getBeginLoc(), "lambda return type deduced to be QStringBuilder instead of QString. Possible crash."); }