in src/checks/level0/temporary-iterator.cpp [54:150]
void TemporaryIterator::VisitStmt(clang::Stmt *stm)
{
auto *memberExpr = dyn_cast<CXXMemberCallExpr>(stm);
if (!memberExpr) {
return;
}
const CXXRecordDecl *classDecl = memberExpr->getRecordDecl();
CXXMethodDecl *methodDecl = memberExpr->getMethodDecl();
if (!classDecl || !methodDecl) {
return;
}
// Check if it's a container
auto it = m_methodsByType.find(clazy::name(classDecl));
if (it == m_methodsByType.end()) {
return;
}
// Check if it's a method returning an iterator
const StringRef functionName = clazy::name(methodDecl);
const auto &allowedFunctions = it->second;
if (!clazy::contains(allowedFunctions, functionName)) {
return;
}
// Catch getList().cbegin().value(), which is ok
if (clazy::getFirstParentOfType<CXXMemberCallExpr>(m_context->parentMap, m_context->parentMap->getParent(memberExpr))) {
return;
}
// Catch variant.toList().cbegin(), which is ok
auto *chainedMemberCall = clazy::getFirstChildOfType<CXXMemberCallExpr>(memberExpr);
if (chainedMemberCall) {
if (isBlacklistedFunction(clazy::qualifiedMethodName(chainedMemberCall->getMethodDecl()))) {
return;
}
}
// catch map[foo].cbegin()
auto *chainedOperatorCall = clazy::getFirstChildOfType<CXXOperatorCallExpr>(memberExpr);
if (chainedOperatorCall) {
FunctionDecl *func = chainedOperatorCall->getDirectCallee();
if (func) {
auto *method = dyn_cast<CXXMethodDecl>(func);
if (method) {
if (isBlacklistedFunction(clazy::qualifiedMethodName(method))) {
return;
}
}
}
}
// If we deref it within the expression, then we'll copy the value before the iterator becomes invalid, so it's safe
if (Utils::isInDerefExpression(memberExpr, m_context->parentMap)) {
return;
}
Expr *expr = memberExpr->getImplicitObjectArgument();
// This check is about detaching temporaries, so check for r value. But with clang20, the same code seems to be an l-value. Check type of expression instead
if (!expr || (expr->isLValue() && !isa<MaterializeTemporaryExpr>(expr))) {
return;
}
const Type *containerType = expr->getType().getTypePtrOrNull();
if (!containerType || containerType->isPointerType()) {
return;
}
{
// *really* check for rvalue
auto *impl = dyn_cast<ImplicitCastExpr>(expr);
if (impl) {
if (impl->getCastKind() == CK_LValueToRValue) {
return;
}
Stmt *firstChild = clazy::getFirstChild(impl);
if (llvm::isa_and_nonnull<ImplicitCastExpr>(firstChild) && dyn_cast<ImplicitCastExpr>(firstChild)->getCastKind() == CK_LValueToRValue) {
return;
}
}
}
auto *possibleCtorCall = dyn_cast_or_null<CXXConstructExpr>(clazy::getFirstChildAtDepth(expr, 2));
if (possibleCtorCall) {
return;
}
auto *possibleThisCall = dyn_cast_or_null<CXXThisExpr>(clazy::getFirstChildAtDepth(expr, 1));
if (possibleThisCall) {
return;
}
std::string error = "Don't call " + clazy::qualifiedMethodName(methodDecl) + "() on temporary";
emitWarning(stm->getBeginLoc(), error);
}