From 32c7b919ba97e123a87b85f28112f129af6c0395 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 29 Sep 2022 07:05:29 +0200 Subject: [PATCH] Add tests for #1201, #2654, #6379 (#4518) * Add test for #6541, avoid duplicate warning * Add test for #5475 * Fix test * Merge * Add test for #8666 * Fix #11239 checkLibraryCheckType with asm goto() (invalid varid) * Format * Add tests for #1201, #2654 * Fix test * Add test for #6379 --- test/testbufferoverrun.cpp | 15 +++++++++++++++ test/testother.cpp | 9 +++++++++ test/teststl.cpp | 8 ++++++++ 3 files changed, 32 insertions(+) diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index e3d84320e..5e488e756 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -5395,6 +5395,21 @@ private: " return p[10];\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("struct X {\n" // #2654 + " int a;\n" + " char b;\n" + "};\n" + "void f() {\n" + " X s;\n" + " int* y = &s.a;\n" + " (void)y[0];\n" + " (void)y[1];\n" + " (void)y[2];\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:9]: (error) The address of local variable 'a' is accessed at non-zero index.\n" + "[test.cpp:7] -> [test.cpp:10]: (error) The address of local variable 'a' is accessed at non-zero index.\n", + errout.str()); } void checkPipeParameterSize() { // #3521 diff --git a/test/testother.cpp b/test/testother.cpp index 38503f6c6..98f6b6b9f 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -8382,6 +8382,15 @@ private: " *reg = 34;\n" "}"); ASSERT_EQUALS("test.cpp:2:style:C-style pointer casting\n", errout.str()); + + check("void f(std::map& m, int key, int value) {\n" // #6379 + " m[key] = value;\n" + " m[key] = value;\n" + "}\n"); + ASSERT_EQUALS("test.cpp:3:style:Variable 'm[key]' is reassigned a value before the old one has been used.\n" + "test.cpp:2:note:m[key] is assigned\n" + "test.cpp:3:note:m[key] is overwritten\n", + errout.str()); } void redundantVarAssignment_trivial() { diff --git a/test/teststl.cpp b/test/teststl.cpp index bc4ed403b..5a3193030 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -5871,6 +5871,14 @@ private: true); ASSERT_EQUALS("[test.cpp:3]: (style) Using sort with iterator 'v.begin()' that is always empty.\n", errout.str()); + check("void f() {\n" // #1201 + " std::vector v1{ 0, 1 };\n" + " std::vector v2;\n" + " std::copy(v1.begin(), v1.end(), v2.begin());\n" + "}\n", + true); + ASSERT_EQUALS("[test.cpp:4]: (style) Using copy with iterator 'v2.begin()' that is always empty.\n", errout.str()); + check("void f() {\n" " std::vector v;\n" " v.insert(v.end(), 1);\n"