diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index a236398a2..06afe5b4b 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4496,12 +4496,15 @@ static void valueFlowForLoopSimplifyAfter(Token *fortok, nonneg int varid, const else endToken = fortok->scope()->bodyEnd; + Token* blockTok = fortok->linkAt(1)->linkAt(1); std::list values; values.emplace_back(num); values.back().errorPath.emplace_back(fortok,"After for loop, " + var->name() + " has value " + values.back().infoString()); - valueFlowForwardVariable( - fortok->linkAt(1)->linkAt(1)->next(), endToken, var, varid, values, false, false, tokenlist, errorLogger, settings); + if (blockTok != endToken) { + valueFlowForwardVariable( + blockTok->next(), endToken, var, varid, values, false, false, tokenlist, errorLogger, settings); + } } static void valueFlowForLoop(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings *settings) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index db243a096..e6bcbfd67 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -4444,6 +4444,14 @@ private: " return r;\n" "}\n"; valueOfTok(code, "0"); + + code = "void fa(int &colors) {\n" + " for (int i = 0; i != 6; ++i) {}\n" + "}\n" + "void fb(not_null parent, int &&colors2) {\n" + " dostuff(1);\n" + "}\n"; + valueOfTok(code, "x"); } void valueFlowCrashConstructorInitialization() { // #9577