Skip to content

Commit

Permalink
Fix #12351 FN constParameterPointer 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 8d4b385 commit 5cceacd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,7 @@ void CheckOther::checkConstPointer()
else if (Token::Match(gparent, "%assign%") && parent == gparent->astOperand2()) {
bool takingRef = false, nonConstPtrAssignment = false;
const Token *lhs = gparent->astOperand1();
if (lhs && lhs->variable() && lhs->variable()->isReference() && lhs->variable()->nameToken() == lhs)
if (lhs && lhs->variable() && lhs->variable()->isReference() && lhs->variable()->nameToken() == lhs && !lhs->variable()->isConst())
takingRef = true;
if (lhs && lhs->valueType() && lhs->valueType()->pointer && (lhs->valueType()->constness & 1) == 0 &&
parent->valueType() && parent->valueType()->pointer)
Expand Down
9 changes: 7 additions & 2 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3480,7 +3480,7 @@ class TestOther : public TestFixture {
"[test.cpp:8]: (style) Parameter 'v' can be declared as reference to const\n",
errout.str());

check("void cb(const std::string&);\n" // #12350
check("void cb(const std::string&);\n" // #12350, #12351
"void f(std::string& s) {\n"
" const std::string& str(s);\n"
" cb(str);\n"
Expand All @@ -3492,10 +3492,15 @@ class TestOther : public TestFixture {
"void h(std::string* s) {\n"
" const std::string& str(*s);\n"
" cb(str);\n"
"}\n"
"void k(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",
"[test.cpp:10]: (style) Parameter 's' can be declared as pointer to const\n"
"[test.cpp:14]: (style) Parameter 's' can be declared as pointer to const\n",
errout.str());
}

Expand Down

0 comments on commit 5cceacd

Please sign in to comment.