From de9b65c7379acfa66da4fd640a1babdaaa6deccb Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 14 Jun 2022 13:05:07 +0200 Subject: [PATCH] Add tests for #9194, #10415, #4759, #9876, #10006 (#4213) * 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 --- test/testautovariables.cpp | 11 ++++++++++ test/testbufferoverrun.cpp | 12 +++++++++++ test/testunusedvar.cpp | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 3da422400..ddc3dc1cb 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -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() diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index aee55054c..e09fdd7ab 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -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() { diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 4c22dd5e3..595762973 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -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"