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() {