Fix #12064 FP useStlAlgorithm with loop over initializer list (#5542)

This commit is contained in:
chrchr-github 2023-10-12 10:06:52 +02:00 committed by GitHub
parent e247e818ec
commit 179d26f06b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

View File

@ -2878,6 +2878,8 @@ void CheckStl::useStlAlgorithm()
loopVar = splitTok->previous();
if (loopVar->varId() == 0)
continue;
if (Token::simpleMatch(splitTok->astOperand2(), "{"))
continue;
}
else { // iterator-based loop?
const Token* initTok = getInitTok(tok);

View File

@ -5437,6 +5437,13 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Consider using std::any_of algorithm instead of a raw loop.\n",
errout.str());
check("void f() {\n" // #12064
" for (const auto& animal : { \"cat\", \"bat\", \"tiger\", \"rat\" })\n"
" if (std::strlen(animal) > 4)\n"
" throw 1;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void loopAlgoMinMax() {