Skip to content

Commit

Permalink
Fix #12762 FN constVariableReference with multiplication (danmar#6432)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed May 23, 2024
1 parent 2263d30 commit fb5679f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,7 @@ static T* getTokenArgumentFunctionImpl(T* tok, int& argn)
parent = parent->astParent();

// passing variable to subfunction?
if (Token::Match(parent, "[*[(,{.]") || Token::Match(parent, "%oror%|&&"))
if (Token::Match(parent, "[[(,{.]") || Token::Match(parent, "%oror%|&&") || (parent && parent->isUnaryOp("*")))
;
else if (Token::simpleMatch(parent, ":")) {
while (Token::Match(parent, "[?:]"))
Expand Down Expand Up @@ -2578,7 +2578,7 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings &settings,

const Token *tok2 = tok;
int derefs = 0;
while (Token::simpleMatch(tok2->astParent(), "*") ||
while ((tok2->astParent() && tok2->astParent()->isUnaryOp("*")) ||
(Token::simpleMatch(tok2->astParent(), ".") && !Token::simpleMatch(tok2->astParent()->astParent(), "(")) ||
(tok2->astParent() && tok2->astParent()->isUnaryOp("&") && Token::simpleMatch(tok2->astParent()->astParent(), ".") && tok2->astParent()->astParent()->originalName()=="->") ||
(Token::simpleMatch(tok2->astParent(), "[") && tok2 == tok2->astParent()->astOperand1())) {
Expand Down
10 changes: 10 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3613,6 +3613,16 @@ class TestOther : public TestFixture {
" });\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 's' can be declared as reference to const\n", errout_str());

check("struct S {\n" // #12762
" std::vector<int> m;\n"
" void f();\n"
"};\n"
"void S::f() {\n"
" std::vector<int>& r = m;\n"
" g(r[0] * 2);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'r' can be declared as reference to const\n", errout_str());
}

void constParameterCallback() {
Expand Down

0 comments on commit fb5679f

Please sign in to comment.