From 005b1cff6103c664501d5e4489a6a475e47893f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 15 Dec 2011 18:15:58 +0100 Subject: [PATCH] Fixed #3417 (False positive: Uninitialized variable when conditionally set) --- lib/checkuninitvar.cpp | 4 ++++ test/testuninitvar.cpp | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 2d518ae8f..f66bf40ae 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1074,6 +1074,10 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int // Inner scope.. if (Token::Match(tok, "if (")) { + if (number_of_if) { + return true; + } + // initialization in condition.. const Token * const endToken = tok->next()->link(); for (const Token *tok2 = tok->tokAt(2); tok2 != endToken; tok2 = tok2->next()) { diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 5bb07622b..d6e853931 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1800,6 +1800,14 @@ private: " for_each(i) { }\n" "}"); ASSERT_EQUALS("", errout.str()); + + // if, if + checkUninitVar2("void f(int a) {\n" + " int i;\n" + " if (a) i = 0;\n" + " if (a) i++;\n" + "}"); + ASSERT_EQUALS("", errout.str()); } };