Skip to content

Commit

Permalink
Fix #12350 FN constParameterReference when taking const reference
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jan 15, 2024
1 parent 5ef846a commit 8d4b385
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2466,6 +2466,11 @@ bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Setti
return true;
}

if (const Variable* var = tok->variable()) {
if (tok == var->nameToken() && var->isConst() && var->isReference())
return false;
}

std::vector<const Variable*> args = getArgumentVars(tok, argnr);
bool conclusive = false;
for (const Variable *arg:args) {
Expand Down
18 changes: 18 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3479,6 +3479,24 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n"
"[test.cpp:8]: (style) Parameter 'v' can be declared as reference to const\n",
errout.str());

check("void cb(const std::string&);\n" // #12350
"void f(std::string& s) {\n"
" const std::string& str(s);\n"
" cb(str);\n"
"}\n"
"void g(std::string& s) {\n"
" const std::string& str{ s };\n"
" cb(str);\n"
"}\n"
"void h(std::string* s) {\n"
" const std::string& str(*s);\n"
" cb(str);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 's' can be declared as reference to const\n"
"[test.cpp:6]: (style) Parameter 's' can be declared as reference to const\n"
"[test.cpp:10]: (style) Parameter 's' can be declared as pointer to const\n",
errout.str());
}

void constParameterCallback() {
Expand Down

0 comments on commit 8d4b385

Please sign in to comment.