Fix issue 9713: FP invalidContainerLoop when modifying container and immediately exiting the loop (#2659)
This commit is contained in:
parent
6162ebd608
commit
4270819728
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue