From 4270819728bd2b40b52a071f3350c24e8550df58 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sat, 23 May 2020 00:14:45 -0500 Subject: [PATCH] Fix issue 9713: FP invalidContainerLoop when modifying container and immediately exiting the loop (#2659) --- lib/checkstl.cpp | 5 +++++ test/teststl.cpp | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 44a89b7e5..25d726af4 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -881,6 +881,11 @@ void CheckStl::invalidContainerLoop() continue; if (!isInvalidMethod(tok2)) continue; + const Scope* s = tok2->scope(); + if (!s) + continue; + if (isReturnScope(s->bodyEnd, &mSettings->library)) + continue; invalidContainerLoopError(tok2, tok); break; } diff --git a/test/teststl.cpp b/test/teststl.cpp index 96206e495..317696b0c 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -4145,6 +4145,19 @@ private: true); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (error) Calling 'push_back' while iterating the container is invalid.\n", errout.str()); + // #9713 + check("void f() {\n" + " std::vector v{1, 2, 3};\n" + " for (int i : v) {\n" + " if (i == 2) {\n" + " v.clear();\n" + " return;\n" + " }\n" + " }\n" + "}\n", + true); + ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::any_of algorithm instead of a raw loop.\n", errout.str()); + } void findInsert() {