diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 498682e76..a9f1446e9 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -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); diff --git a/test/teststl.cpp b/test/teststl.cpp index f81bea7d6..b545a52fc 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -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() {