From 3a4f3aa5a2618cd6a35ad9e50b6c1381ab98da34 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Fri, 5 Jun 2026 21:08:59 +0200 Subject: [PATCH] Fix #14817 FN constParameterPointer for constructor initializing array (regression) , add test for #11471 --- lib/astutils.cpp | 2 ++ test/testother.cpp | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 8dfd03596d3..7af806508fd 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2592,6 +2592,8 @@ bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Setti if (const Variable* var = tok->variable()) { if (tok == var->nameToken() && (!var->isReference() || (var->isConst() && var->type() == tok1->type())) && (!var->isClass() || (var->valueType() && var->valueType()->container))) // const ref or passed to (copy) ctor return false; + if (var->isArray() && var->valueType() && var->valueType()->pointer == 0 && var->valueType()->isPrimitive()) + return false; } std::vector args = getArgumentVars(tok, argnr); diff --git a/test/testother.cpp b/test/testother.cpp index 93e67229f43..4d2b775c3d3 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4864,6 +4864,28 @@ class TestOther : public TestFixture { ASSERT_EQUALS("[test.cpp:1:12]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:1:20]: (style) Parameter 'q' can be declared as pointer to const [constParameterPointer]\n", errout_str()); + + check("struct S {\n" // #14817 + " explicit S(int *a) : m{ a[0], a[1] } {}\n" + " int m[2];\n" + "}" + "struct T {\n" + " explicit T(int *a) : m{ &a[0], &a[1] } {}\n" + " int* m[2];\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:2:21]: (style) Parameter 'a' can be declared as pointer to const [constParameterPointer]\n", + errout_str()); + + check("class A {\n" // #11471 + "public:\n" + " A(const int& i, int) : m_i(&i) {}\n" + " const int* m_i;\n" + "};\n" + "A f(int& s) {\n" + " return A(s, 0);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:6:10]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n", + errout_str()); } void constArray() {