* Add test for #10152

* Add test for #9773

* Fix test

* Add test for #7529

* Add test for #6371

* Add test for #6475

* Format

* Format

* Fix test

* Remove duplicate test

* Add valueflow test

* Rebuild

* Add tests for #9194, #10415, #4759, #9876, #10006
This commit is contained in:
chrchr-github 2022-06-14 13:05:07 +02:00 committed by GitHub
parent 6d22d6a8ae
commit de9b65c737
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 0 deletions

View File

@ -3584,6 +3584,17 @@ private:
" (void)cargs;\n"
"};\n");
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());
check("struct C {\n" // #9194
" const int& m;\n"
" C(const int& i) : m(i) {}\n"
" int get() { return m; }\n"
"};\n"
"int f() {\n"
" C c(42);\n"
" return c.get();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:7] -> [test.cpp:8]: (error) Using object that is a temporary.\n", errout.str());
}
void danglingLifetimeBorrowedMembers()

View File

@ -4902,6 +4902,18 @@ private:
" dostuff(s);\n"
"}");
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());
ctu("void f(int* p) {\n" // #10415
" int b[1];\n"
" b[0] = p[5];\n"
" std::cout << b[0];\n"
"}\n"
"void g() {\n"
" int* a = new int[1];\n"
" a[0] = 5;\n"
" f(a);\n"
"}\n");
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());
}
void ctu_array() {

View File

@ -68,6 +68,7 @@ private:
TEST_CASE(structmember18); // #10684
TEST_CASE(structmember19); // #10826, #10848, #10852
TEST_CASE(structmember20); // #10737
TEST_CASE(structmember21); // #4759
TEST_CASE(localvar1);
TEST_CASE(localvar2);
@ -132,6 +133,7 @@ private:
TEST_CASE(localvar62); // #10824
TEST_CASE(localvar63); // #6928
TEST_CASE(localvar64); // #9997
TEST_CASE(localvar65); // #9876, #10006
TEST_CASE(localvarloops); // loops
TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637
@ -1772,6 +1774,27 @@ private:
ASSERT_EQUALS("", errout.str());
}
void structmember21() { // #4759
checkStructMemberUsage("class C {\n"
"public:\n"
" int f() { return 0; }\n"
"};\n"
"C C;\n"
"int g() {\n"
" return c.f();\n"
"}\n"
"struct S {\n"
" int f;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:10]: (style) struct member 'S::f' is never used.\n", errout.str());
checkStructMemberUsage("struct A { int i; };\n"
"struct B { struct A* pA; };");
ASSERT_EQUALS("[test.cpp:1]: (style) struct member 'A::i' is never used.\n"
"[test.cpp:2]: (style) struct member 'B::pA' is never used.\n",
errout.str());
}
void functionVariableUsage_(const char* file, int line, const char code[], const char filename[] = "test.cpp") {
// Clear the error buffer..
errout.str("");
@ -3518,6 +3541,25 @@ private:
errout.str());
}
void localvar65() {
functionVariableUsage("bool b();\n" // #9876
"void f() {\n"
" for (;;) {\n"
" const T* t = tok->next()->link()->next();\n"
" if (!b())\n"
" continue;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 't' is assigned a value that is never used.\n", errout.str());
functionVariableUsage("void f() {\n" // #10006
" std::string s = \"\";\n"
" try {}\n"
" catch (...) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout.str());
}
void localvarloops() {
// loops
functionVariableUsage("void fun(int c) {\n"