diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index ec90fb3e302..09e86b1cb3c 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4531,7 +4531,7 @@ struct ConditionHandler { } // Variable changed in loop code - const Token* const start = top; + const Token* const start = top->strAt(-1) == "for" ? top->astOperand2() : top; // skip init statement const Token* const block = top->link()->next(); const Token* const end = block->link(); diff --git a/test/teststl.cpp b/test/teststl.cpp index c24bf2a5fb1..cc4856628df 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -1010,6 +1010,12 @@ class TestStl : public TestFixture { ASSERT_EQUALS( "[test.cpp:2:12] -> [test.cpp:4:21]: (warning) Either the condition 'col>textline.size()' is redundant or 'col' can have the value textline.size(). Expression 'textline[col]' causes access out of bounds. [containerOutOfBounds]\n", errout_str()); + + check("void f(const std::vector& v) {\n" // #12742 + " for (unsigned i = 0; i < v.size();)\n" + " (void)v[i++];\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); } void outOfBoundsIndexExpression() {