From 39ec2d50928cee393198e0da506f8bae2d8f6405 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 14 Aug 2024 21:52:23 +0200 Subject: [PATCH] avoid using temporary objects in `followAllReferences()` --- lib/astutils.cpp | 24 ++++++++++++------------ lib/astutils.h | 4 ++++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 77830788b40..5aaf2722898 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1250,27 +1250,27 @@ SmallVector followAllReferences(const Token* tok, return {}; if (depth < 0) { SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); + refs_result.emplace_back(tok, std::move(errors)); return refs_result; } const Variable *var = tok->variable(); if (var && var->declarationId() == tok->varId()) { if (var->nameToken() == tok || isStructuredBindingVariable(var)) { SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); + refs_result.emplace_back(tok, std::move(errors)); return refs_result; } if (var->isReference() || var->isRValueReference()) { const Token * const varDeclEndToken = var->declEndToken(); if (!varDeclEndToken) { SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); + refs_result.emplace_back(tok, std::move(errors)); return refs_result; } if (var->isArgument()) { errors.emplace_back(varDeclEndToken, "Passed to reference."); SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); + refs_result.emplace_back(tok, std::move(errors)); return refs_result; } if (Token::simpleMatch(varDeclEndToken, "=")) { @@ -1281,7 +1281,7 @@ SmallVector followAllReferences(const Token* tok, if (vartok == tok || (!temporary && isTemporary(vartok, nullptr, true) && (var->isConst() || var->isRValueReference()))) { SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); + refs_result.emplace_back(tok, std::move(errors)); return refs_result; } if (vartok) @@ -1299,7 +1299,7 @@ SmallVector followAllReferences(const Token* tok, if (!inconclusive && result.size() != 1) { SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); + refs_result.emplace_back(tok, std::move(errors)); return refs_result; } @@ -1313,7 +1313,7 @@ SmallVector followAllReferences(const Token* tok, const Function *f = tok->previous()->function(); if (!Function::returnsReference(f)) { SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); + refs_result.emplace_back(tok, std::move(errors)); return refs_result; } std::set result; @@ -1326,20 +1326,20 @@ SmallVector followAllReferences(const Token* tok, const Variable* argvar = rt.token->variable(); if (!argvar) { SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); + refs_result.emplace_back(tok, std::move(errors)); return refs_result; } if (argvar->isArgument() && (argvar->isReference() || argvar->isRValueReference())) { const int n = getArgumentPos(argvar, f); if (n < 0) { SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); + refs_result.emplace_back(tok, std::move(errors)); return refs_result; } std::vector args = getArguments(tok->previous()); if (n >= args.size()) { SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); + refs_result.emplace_back(tok, std::move(errors)); return refs_result; } const Token* argTok = args[n]; @@ -1351,7 +1351,7 @@ SmallVector followAllReferences(const Token* tok, result.insert(refs.cbegin(), refs.cend()); if (!inconclusive && result.size() > 1) { SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); + refs_result.emplace_back(tok, std::move(errors)); return refs_result; } } @@ -1364,7 +1364,7 @@ SmallVector followAllReferences(const Token* tok, } } SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); + refs_result.emplace_back(tok, std::move(errors)); return refs_result; } diff --git a/lib/astutils.h b/lib/astutils.h index faaddc99e23..5c9427d6e79 100644 --- a/lib/astutils.h +++ b/lib/astutils.h @@ -252,6 +252,10 @@ bool succeeds(const Token* tok1, const Token* tok2); bool exprDependsOnThis(const Token* expr, bool onVar = true, nonneg int depth = 0); struct ReferenceToken { + ReferenceToken(const Token* t, ErrorPath e) + : token(t) + , errors(std::move(e)) + {} const Token* token; ErrorPath errors; };