From 065853a59aec55395e30be39263619ff1dc01c95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 29 May 2013 16:16:12 +0200 Subject: [PATCH] Fixed #4652 (False positive: variable value tracking into loop body) --- lib/checkuninitvar.cpp | 5 ++--- test/testuninitvar.cpp | 9 +++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 6817fdbf1..7cb672fc4 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1327,11 +1327,10 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, const Token *tok2 = tok->next()->link()->next(); if (tok2 && tok2->str() == "{") { - bool possibleinit = false; - bool init = checkLoopBody(tok2, var, membervar, suppressErrors); + bool init = checkLoopBody(tok2, var, membervar, (number_of_if > 0) | suppressErrors); // variable is initialized in the loop.. - if (possibleinit || init) + if (init) return true; // is variable used in for-head? diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 72e39d612..6f1cb80b6 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2618,6 +2618,15 @@ private: " i++;\n" // <- no error if b(x) is always true when a(x) is false "}"); ASSERT_EQUALS("", errout.str()); + + checkUninitVar2("void f(int x) {\n" + " int i;\n" + " if (x) i = 0;\n" + " while (condition) {\n" + " if (x) i++;\n" // <- no error + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void uninitvar2_structmembers() { // struct members