Skip to content

Commit

Permalink
Fix #12086 FN passedByValue with index operator and namespaced functi…
Browse files Browse the repository at this point in the history
…on call (danmar#5574)
  • Loading branch information
chrchr-github authored Oct 21, 2023
1 parent f4d18a8 commit 26ba29c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
20 changes: 2 additions & 18 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
2 changes: 1 addition & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2159,6 +2159,14 @@ class TestOther : public TestFixture {
"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",
Expand Down

0 comments on commit 26ba29c

Please sign in to comment.