Skip to content

Commit

Permalink
Fix #12064 FP useStlAlgorithm with loop over initializer list
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Oct 11, 2023
1 parent fe8730c commit 45ab306
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5437,6 +5437,13 @@ class TestStl : public TestFixture {
"}\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() {
Expand Down

0 comments on commit 45ab306

Please sign in to comment.