Skip to content

Commit de9b65c

Browse files
Add tests for #9194, #10415, cppcheck-opensource#4759, #9876, #10006 (cppcheck-opensource#4213)
* Add test for #10152 * Add test for #9773 * Fix test * Add test for cppcheck-opensource#7529 * Add test for cppcheck-opensource#6371 * Add test for cppcheck-opensource#6475 * Format * Format * Fix test * Remove duplicate test * Add valueflow test * Rebuild * Add tests for #9194, #10415, cppcheck-opensource#4759, #9876, #10006
1 parent 6d22d6a commit de9b65c

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

test/testautovariables.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3584,6 +3584,17 @@ class TestAutoVariables : public TestFixture {
35843584
" (void)cargs;\n"
35853585
"};\n");
35863586
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:4] -> [test.cpp:3] -> [test.cpp:1] -> [test.cpp:4] -> [test.cpp:9] -> [test.cpp:9] -> [test.cpp:10]: (error) Using object that is a temporary.\n", errout.str());
3587+
3588+
check("struct C {\n" // #9194
3589+
" const int& m;\n"
3590+
" C(const int& i) : m(i) {}\n"
3591+
" int get() { return m; }\n"
3592+
"};\n"
3593+
"int f() {\n"
3594+
" C c(42);\n"
3595+
" return c.get();\n"
3596+
"}\n");
3597+
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:7] -> [test.cpp:8]: (error) Using object that is a temporary.\n", errout.str());
35873598
}
35883599

35893600
void danglingLifetimeBorrowedMembers()

test/testbufferoverrun.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4902,6 +4902,18 @@ class TestBufferOverrun : public TestFixture {
49024902
" dostuff(s);\n"
49034903
"}");
49044904
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:7] -> [test.cpp:2]: (error) Array index out of bounds; 'p' buffer size is 4 and it is accessed at offset 4.\n", errout.str());
4905+
4906+
ctu("void f(int* p) {\n" // #10415
4907+
" int b[1];\n"
4908+
" b[0] = p[5];\n"
4909+
" std::cout << b[0];\n"
4910+
"}\n"
4911+
"void g() {\n"
4912+
" int* a = new int[1];\n"
4913+
" a[0] = 5;\n"
4914+
" f(a);\n"
4915+
"}\n");
4916+
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:9] -> [test.cpp:3]: (error) Array index out of bounds; 'p' buffer size is 4 and it is accessed at offset 20.\n", errout.str());
49054917
}
49064918

49074919
void ctu_array() {

test/testunusedvar.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class TestUnusedVar : public TestFixture {
6868
TEST_CASE(structmember18); // #10684
6969
TEST_CASE(structmember19); // #10826, #10848, #10852
7070
TEST_CASE(structmember20); // #10737
71+
TEST_CASE(structmember21); // #4759
7172

7273
TEST_CASE(localvar1);
7374
TEST_CASE(localvar2);
@@ -132,6 +133,7 @@ class TestUnusedVar : public TestFixture {
132133
TEST_CASE(localvar62); // #10824
133134
TEST_CASE(localvar63); // #6928
134135
TEST_CASE(localvar64); // #9997
136+
TEST_CASE(localvar65); // #9876, #10006
135137
TEST_CASE(localvarloops); // loops
136138
TEST_CASE(localvaralias1);
137139
TEST_CASE(localvaralias2); // ticket #1637
@@ -1772,6 +1774,27 @@ class TestUnusedVar : public TestFixture {
17721774
ASSERT_EQUALS("", errout.str());
17731775
}
17741776

1777+
void structmember21() { // #4759
1778+
checkStructMemberUsage("class C {\n"
1779+
"public:\n"
1780+
" int f() { return 0; }\n"
1781+
"};\n"
1782+
"C C;\n"
1783+
"int g() {\n"
1784+
" return c.f();\n"
1785+
"}\n"
1786+
"struct S {\n"
1787+
" int f;\n"
1788+
"};\n");
1789+
ASSERT_EQUALS("[test.cpp:10]: (style) struct member 'S::f' is never used.\n", errout.str());
1790+
1791+
checkStructMemberUsage("struct A { int i; };\n"
1792+
"struct B { struct A* pA; };");
1793+
ASSERT_EQUALS("[test.cpp:1]: (style) struct member 'A::i' is never used.\n"
1794+
"[test.cpp:2]: (style) struct member 'B::pA' is never used.\n",
1795+
errout.str());
1796+
}
1797+
17751798
void functionVariableUsage_(const char* file, int line, const char code[], const char filename[] = "test.cpp") {
17761799
// Clear the error buffer..
17771800
errout.str("");
@@ -3518,6 +3541,25 @@ class TestUnusedVar : public TestFixture {
35183541
errout.str());
35193542
}
35203543

3544+
void localvar65() {
3545+
functionVariableUsage("bool b();\n" // #9876
3546+
"void f() {\n"
3547+
" for (;;) {\n"
3548+
" const T* t = tok->next()->link()->next();\n"
3549+
" if (!b())\n"
3550+
" continue;\n"
3551+
" }\n"
3552+
"}\n");
3553+
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 't' is assigned a value that is never used.\n", errout.str());
3554+
3555+
functionVariableUsage("void f() {\n" // #10006
3556+
" std::string s = \"\";\n"
3557+
" try {}\n"
3558+
" catch (...) {}\n"
3559+
"}\n");
3560+
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout.str());
3561+
}
3562+
35213563
void localvarloops() {
35223564
// loops
35233565
functionVariableUsage("void fun(int c) {\n"

0 commit comments

Comments
 (0)