Fix issue 9713: FP invalidContainerLoop when modifying container and immediately exiting the loop (#2659)

This commit is contained in:
Paul Fultz II 2020-05-23 00:14:45 -05:00 committed by GitHub
parent 6162ebd608
commit 4270819728
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -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;
}

View File

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