Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,8 @@ void CheckOther::checkConstPointer()
continue;
if (deref != NONE) {
const Token* gparent = parent->astParent();
while (Token::simpleMatch(gparent, "[") && parent->str() == gparent->str())
gparent = gparent->astParent();
if (deref == MEMBER) {
if (!gparent)
continue;
Expand Down
82 changes: 48 additions & 34 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class TestOther : public TestFixture {
TEST_CASE(constVariable);
TEST_CASE(constParameterCallback);
TEST_CASE(constPointer);
TEST_CASE(constArray);

TEST_CASE(switchRedundantAssignmentTest);
TEST_CASE(switchRedundantOperationTest);
Expand Down Expand Up @@ -3394,14 +3395,6 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("", errout_str());

check("void f(std::array<int, 2>& a) {\n"
" if (a[0]) {}\n"
"}\n"
"void g(std::array<int, 2>& a) {\n"
" a.fill(0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'a' can be declared as const array\n", errout_str());

// #11682
check("struct b {\n"
" void mutate();\n"
Expand Down Expand Up @@ -3823,19 +3816,6 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("", errout_str());

check("int f() {\n"
" static int i[1] = {};\n"
" return i[0];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'i' can be declared as const array\n", errout_str());

check("int f() {\n"
" static int i[] = { 0 };\n"
" int j = i[0] + 1;\n"
" return j;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'i' can be declared as const array\n", errout_str());

// #10471
check("void f(std::array<int, 1> const& i) {\n"
" if (i[0] == 0) {}\n"
Expand Down Expand Up @@ -3913,19 +3893,6 @@ class TestOther : public TestFixture {
"S<int*> s;\n");
ASSERT_EQUALS("", errout_str());

check("void f(int i) {\n"
" const char *tmp;\n"
" char* a[] = { \"a\", \"aa\" };\n"
" static char* b[] = { \"b\", \"bb\" };\n"
" tmp = a[i];\n"
" printf(\"%s\", tmp);\n"
" tmp = b[i];\n"
" printf(\"%s\", tmp);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' can be declared as const array\n"
"[test.cpp:4]: (style) Variable 'b' can be declared as const array\n",
errout_str());

check("typedef void* HWND;\n" // #11084
"void f(const HWND h) {\n"
" if (h == nullptr) {}\n"
Expand Down Expand Up @@ -4228,6 +4195,53 @@ class TestOther : public TestFixture {
errout_str());
}

void constArray() {
check("void f(std::array<int, 2>& a) {\n"
" if (a[0]) {}\n"
"}\n"
"void g(std::array<int, 2>& a) {\n"
" a.fill(0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'a' can be declared as const array\n", errout_str());

check("int f() {\n"
" static int i[1] = {};\n"
" return i[0];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'i' can be declared as const array\n", errout_str());

check("int f() {\n"
" static int i[] = { 0 };\n"
" int j = i[0] + 1;\n"
" return j;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'i' can be declared as const array\n", errout_str());

check("void f(int i) {\n"
" const char *tmp;\n"
" char* a[] = { \"a\", \"aa\" };\n"
" static char* b[] = { \"b\", \"bb\" };\n"
" tmp = a[i];\n"
" printf(\"%s\", tmp);\n"
" tmp = b[i];\n"
" printf(\"%s\", tmp);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' can be declared as const array\n"
"[test.cpp:4]: (style) Variable 'b' can be declared as const array\n",
errout_str());

check("int f(int i, int j) {\n" // #13069
" int a[3][4] = {\n"
" { 2, 2, -1, -1 },\n"
" { 2, -1, 2, -1 },\n"
" { 2, -1, -1, 2 },\n"
" };\n"
" return a[j][i];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'a' can be declared as const array\n",
errout_str());
}

void switchRedundantAssignmentTest() {
check("void foo()\n"
"{\n"
Expand Down