Skip to content

Commit 3a4f3aa

Browse files
committed
Fix #14817 FN constParameterPointer for constructor initializing array (regression) , add test for #11471
1 parent 2285c1d commit 3a4f3aa

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

lib/astutils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,8 @@ bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Setti
25922592
if (const Variable* var = tok->variable()) {
25932593
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
25942594
return false;
2595+
if (var->isArray() && var->valueType() && var->valueType()->pointer == 0 && var->valueType()->isPrimitive())
2596+
return false;
25952597
}
25962598

25972599
std::vector<const Variable*> args = getArgumentVars(tok, argnr);

test/testother.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4864,6 +4864,28 @@ class TestOther : public TestFixture {
48644864
ASSERT_EQUALS("[test.cpp:1:12]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n"
48654865
"[test.cpp:1:20]: (style) Parameter 'q' can be declared as pointer to const [constParameterPointer]\n",
48664866
errout_str());
4867+
4868+
check("struct S {\n" // #14817
4869+
" explicit S(int *a) : m{ a[0], a[1] } {}\n"
4870+
" int m[2];\n"
4871+
"}"
4872+
"struct T {\n"
4873+
" explicit T(int *a) : m{ &a[0], &a[1] } {}\n"
4874+
" int* m[2];\n"
4875+
"};\n");
4876+
ASSERT_EQUALS("[test.cpp:2:21]: (style) Parameter 'a' can be declared as pointer to const [constParameterPointer]\n",
4877+
errout_str());
4878+
4879+
check("class A {\n" // #11471
4880+
"public:\n"
4881+
" A(const int& i, int) : m_i(&i) {}\n"
4882+
" const int* m_i;\n"
4883+
"};\n"
4884+
"A f(int& s) {\n"
4885+
" return A(s, 0);\n"
4886+
"}\n");
4887+
ASSERT_EQUALS("[test.cpp:6:10]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n",
4888+
errout_str());
48674889
}
48684890

48694891
void constArray() {

0 commit comments

Comments
 (0)