From fb5679f9a7feb627b297fd0e929a48c0622f5313 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 23 May 2024 20:21:12 +0200 Subject: [PATCH] Fix #12762 FN constVariableReference with multiplication (#6432) --- lib/astutils.cpp | 4 ++-- test/testother.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 4d63dc8135a..efe879e180d 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -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, "[?:]")) @@ -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())) { diff --git a/test/testother.cpp b/test/testother.cpp index 081c69e06b2..ad727af084f 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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 m;\n" + " void f();\n" + "};\n" + "void S::f() {\n" + " std::vector& 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() {