diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 200b9cfea..94f572abf 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1584,7 +1584,7 @@ void CheckStl::readingEmptyStlContainer() else readingEmptyStlContainer_parseUsage(tok2, false, empty_nonmap, true); } - } else if (Token::Match(tok, "do|}")) { + } else if (Token::Match(tok, "do|}|break|case")) { empty_map.clear(); empty_nonmap.clear(); } diff --git a/test/teststl.cpp b/test/teststl.cpp index bd0458a9f..9e50413a4 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -2677,8 +2677,54 @@ private: " std::set container;\n" " while (container.size() < 5)\n" " container.insert(22);\n" - "}"); + "}", true); ASSERT_EQUALS("", errout.str()); + + // #6679 + check("class C {\n" + " C() {\n" + " switch (ret) {\n" + " case 1:\n" + " vec.clear();\n" + " break;\n" + " case 2:\n" + " if (vec.empty())\n" + " ;\n" + " break;\n" + " }\n" + " }\n" + " std::vector vec;\n" + "};", true); + ASSERT_EQUALS("", errout.str()); + + check("class C {\n" + " C() {\n" + " switch (ret) {\n" + " case 1:\n" + " vec.clear();\n" + " case 2:\n" + " if (vec.empty())\n" + " ;\n" + " break;\n" + " }\n" + " }\n" + " std::vector vec;\n" + "};", true); + ASSERT_EQUALS("", errout.str()); + + check("class C {\n" + " C() {\n" + " switch (ret) {\n" + " case 1:\n" + " vec.clear();\n" + " if (vec.empty())\n" + " ;\n" + " break;\n" + " }\n" + " }\n" + " std::vector vec;\n" + "};", true); + ASSERT_EQUALS("[test.cpp:6]: (style, inconclusive) Reading from empty STL container 'vec'\n", errout.str()); } };