From 45ab3067466e02719c0ddb91e5d3650f4bb91004 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Wed, 11 Oct 2023 22:10:46 +0200 Subject: [PATCH] Fix #12064 FP useStlAlgorithm with loop over initializer list --- lib/checkstl.cpp | 2 ++ test/teststl.cpp | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index dd611356df8..e7196d71c50 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..a44a2339a14 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() {