Fix #12086 FN passedByValue with index operator and namespaced function call (#5574)

This commit is contained in:
chrchr-github 2023-10-21 12:41:39 +02:00 committed by GitHub
parent f4d18a8d1e
commit 26ba29c303
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 21 deletions

View File

@ -3052,7 +3052,7 @@ const Token* getIteratorExpression(const Token* tok)
return nullptr;
}
bool isIteratorPair(std::vector<const Token*> args)
bool isIteratorPair(const std::vector<const Token*>& args)
{
if (args.size() != 2)
return false;

View File

@ -393,7 +393,7 @@ const Token* getIteratorExpression(const Token* tok);
/**
* Are the arguments a pair of iterators/pointers?
*/
bool isIteratorPair(std::vector<const Token*> args);
bool isIteratorPair(const std::vector<const Token*>& args);
CPPCHECKLIB const Token *findLambdaStartToken(const Token *last);

View File

@ -1281,27 +1281,11 @@ static bool canBeConst(const Variable *var, const Settings* settings)
if (parent->str() == ">>" && parent->astOperand2() == tok2)
return false;
} else if (parent->str() == "," || parent->str() == "(") { // function argument
const Token* tok3 = tok2->previous();
int argNr = 0;
while (tok3 && tok3->str() != "(") {
if (tok3->link() && Token::Match(tok3, ")|]|}|>"))
tok3 = tok3->link();
else if (tok3->link())
break;
else if (tok3->str() == ";")
break;
else if (tok3->str() == ",")
argNr++;
tok3 = tok3->previous();
}
if (!tok3 || tok3->str() != "(")
return false;
const Token* functionTok = tok3->astOperand1();
int argNr = -1;
const Token* functionTok = getTokenArgumentFunction(tok2, argNr);
if (!functionTok)
return false;
const Function* tokFunction = functionTok->function();
if (!tokFunction && functionTok->str() == "." && (functionTok = functionTok->astOperand2()))
tokFunction = functionTok->function();
if (tokFunction) {
const Variable* argVar = tokFunction->getArgumentVar(argNr);
if (!argVar || (!argVar->isConst() && argVar->isReference()))

View File

@ -4280,7 +4280,7 @@ static bool hasBorrowingVariables(const std::list<Variable>& vars, const std::ve
static void valueFlowLifetimeUserConstructor(Token* tok,
const Function* constructor,
const std::string& name,
std::vector<const Token*> args,
const std::vector<const Token*>& args,
TokenList& tokenlist,
ErrorLogger* errorLogger,
const Settings* settings)

View File

@ -2159,6 +2159,14 @@ private:
"T::T(std::string s) noexcept(true) : m(std::move(s)) {}\n");
ASSERT_EQUALS("", errout.str());
check("namespace N {\n" // #12086
" void g(int);\n"
"}\n"
"void f(std::vector<int> v) {\n"
" N::g(v[0]);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Function parameter 'v' should be passed by const reference.\n", errout.str());
Settings settings1 = settingsBuilder().platform(Platform::Type::Win64).build();
check("using ui64 = unsigned __int64;\n"
"ui64 Test(ui64 one, ui64 two) { return one + two; }\n",