Skip to content

Commit e3a5d41

Browse files
Fix #13069 FN constVariable for multidimensional array (#6771)
1 parent b8b0280 commit e3a5d41

2 files changed

Lines changed: 50 additions & 34 deletions

File tree

lib/checkother.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,8 @@ void CheckOther::checkConstPointer()
16581658
continue;
16591659
if (deref != NONE) {
16601660
const Token* gparent = parent->astParent();
1661+
while (Token::simpleMatch(gparent, "[") && parent->str() == gparent->str())
1662+
gparent = gparent->astParent();
16611663
if (deref == MEMBER) {
16621664
if (!gparent)
16631665
continue;

test/testother.cpp

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class TestOther : public TestFixture {
114114
TEST_CASE(constVariable);
115115
TEST_CASE(constParameterCallback);
116116
TEST_CASE(constPointer);
117+
TEST_CASE(constArray);
117118

118119
TEST_CASE(switchRedundantAssignmentTest);
119120
TEST_CASE(switchRedundantOperationTest);
@@ -3394,14 +3395,6 @@ class TestOther : public TestFixture {
33943395
"}\n");
33953396
ASSERT_EQUALS("", errout_str());
33963397

3397-
check("void f(std::array<int, 2>& a) {\n"
3398-
" if (a[0]) {}\n"
3399-
"}\n"
3400-
"void g(std::array<int, 2>& a) {\n"
3401-
" a.fill(0);\n"
3402-
"}\n");
3403-
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'a' can be declared as const array\n", errout_str());
3404-
34053398
// #11682
34063399
check("struct b {\n"
34073400
" void mutate();\n"
@@ -3823,19 +3816,6 @@ class TestOther : public TestFixture {
38233816
"}\n");
38243817
ASSERT_EQUALS("", errout_str());
38253818

3826-
check("int f() {\n"
3827-
" static int i[1] = {};\n"
3828-
" return i[0];\n"
3829-
"}\n");
3830-
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'i' can be declared as const array\n", errout_str());
3831-
3832-
check("int f() {\n"
3833-
" static int i[] = { 0 };\n"
3834-
" int j = i[0] + 1;\n"
3835-
" return j;\n"
3836-
"}\n");
3837-
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'i' can be declared as const array\n", errout_str());
3838-
38393819
// #10471
38403820
check("void f(std::array<int, 1> const& i) {\n"
38413821
" if (i[0] == 0) {}\n"
@@ -3913,19 +3893,6 @@ class TestOther : public TestFixture {
39133893
"S<int*> s;\n");
39143894
ASSERT_EQUALS("", errout_str());
39153895

3916-
check("void f(int i) {\n"
3917-
" const char *tmp;\n"
3918-
" char* a[] = { \"a\", \"aa\" };\n"
3919-
" static char* b[] = { \"b\", \"bb\" };\n"
3920-
" tmp = a[i];\n"
3921-
" printf(\"%s\", tmp);\n"
3922-
" tmp = b[i];\n"
3923-
" printf(\"%s\", tmp);\n"
3924-
"}\n");
3925-
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' can be declared as const array\n"
3926-
"[test.cpp:4]: (style) Variable 'b' can be declared as const array\n",
3927-
errout_str());
3928-
39293896
check("typedef void* HWND;\n" // #11084
39303897
"void f(const HWND h) {\n"
39313898
" if (h == nullptr) {}\n"
@@ -4228,6 +4195,53 @@ class TestOther : public TestFixture {
42284195
errout_str());
42294196
}
42304197

4198+
void constArray() {
4199+
check("void f(std::array<int, 2>& a) {\n"
4200+
" if (a[0]) {}\n"
4201+
"}\n"
4202+
"void g(std::array<int, 2>& a) {\n"
4203+
" a.fill(0);\n"
4204+
"}\n");
4205+
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'a' can be declared as const array\n", errout_str());
4206+
4207+
check("int f() {\n"
4208+
" static int i[1] = {};\n"
4209+
" return i[0];\n"
4210+
"}\n");
4211+
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'i' can be declared as const array\n", errout_str());
4212+
4213+
check("int f() {\n"
4214+
" static int i[] = { 0 };\n"
4215+
" int j = i[0] + 1;\n"
4216+
" return j;\n"
4217+
"}\n");
4218+
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'i' can be declared as const array\n", errout_str());
4219+
4220+
check("void f(int i) {\n"
4221+
" const char *tmp;\n"
4222+
" char* a[] = { \"a\", \"aa\" };\n"
4223+
" static char* b[] = { \"b\", \"bb\" };\n"
4224+
" tmp = a[i];\n"
4225+
" printf(\"%s\", tmp);\n"
4226+
" tmp = b[i];\n"
4227+
" printf(\"%s\", tmp);\n"
4228+
"}\n");
4229+
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' can be declared as const array\n"
4230+
"[test.cpp:4]: (style) Variable 'b' can be declared as const array\n",
4231+
errout_str());
4232+
4233+
check("int f(int i, int j) {\n" // #13069
4234+
" int a[3][4] = {\n"
4235+
" { 2, 2, -1, -1 },\n"
4236+
" { 2, -1, 2, -1 },\n"
4237+
" { 2, -1, -1, 2 },\n"
4238+
" };\n"
4239+
" return a[j][i];\n"
4240+
"}\n");
4241+
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'a' can be declared as const array\n",
4242+
errout_str());
4243+
}
4244+
42314245
void switchRedundantAssignmentTest() {
42324246
check("void foo()\n"
42334247
"{\n"

0 commit comments

Comments
 (0)