From 179d26f06bdba9ca292cd5ef19075869802cc603 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 12 Oct 2023 10:06:52 +0200 Subject: [PATCH] Fix #12064 FP useStlAlgorithm with loop over initializer list (#5542) --- lib/checkstl.cpp | 2 ++ test/teststl.cpp | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 498682e761f..a9f1446e997 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 f81bea7d698..b545a52fc70 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -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() {