Skip to content

Commit 179d26f

Browse files
Fix #12064 FP useStlAlgorithm with loop over initializer list (#5542)
1 parent e247e81 commit 179d26f

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

lib/checkstl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,6 +2878,8 @@ void CheckStl::useStlAlgorithm()
28782878
loopVar = splitTok->previous();
28792879
if (loopVar->varId() == 0)
28802880
continue;
2881+
if (Token::simpleMatch(splitTok->astOperand2(), "{"))
2882+
continue;
28812883
}
28822884
else { // iterator-based loop?
28832885
const Token* initTok = getInitTok(tok);

test/teststl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5437,6 +5437,13 @@ class TestStl : public TestFixture {
54375437
"}\n");
54385438
ASSERT_EQUALS("[test.cpp:2]: (style) Consider using std::any_of algorithm instead of a raw loop.\n",
54395439
errout.str());
5440+
5441+
check("void f() {\n" // #12064
5442+
" for (const auto& animal : { \"cat\", \"bat\", \"tiger\", \"rat\" })\n"
5443+
" if (std::strlen(animal) > 4)\n"
5444+
" throw 1;\n"
5445+
"}\n");
5446+
ASSERT_EQUALS("", errout.str());
54405447
}
54415448

54425449
void loopAlgoMinMax() {

0 commit comments

Comments
 (0)