From 965c1a94fd039b4916d14000003719fc45d84802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 17 Jan 2011 20:51:15 +0100 Subject: [PATCH] Fixed #2475 (False positive in structure initialisation: The scope of the variable bits can be reduced) --- lib/checkother.cpp | 12 +++++++++++- test/testother.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 768f8b63c..33465af8b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2008,7 +2008,17 @@ void CheckOther::lookupVar(const Token *tok1, const std::string &varname) { if (tok->str() == "{") { - ++indentlevel; + if (tok->strAt(-1) == "=") + { + if (Token::findmatch(tok, varname.c_str(), tok->link())) + { + return; + } + + tok = tok->link(); + } + else + ++indentlevel; } else if (tok->str() == "}") diff --git a/test/testother.cpp b/test/testother.cpp index 35e4e5e2a..935c7df32 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -58,6 +58,7 @@ private: TEST_CASE(varScope8); TEST_CASE(varScope9); // classes may have extra side-effects TEST_CASE(varScope10); // Undefined macro FOR + TEST_CASE(varScope11); // #2475 - struct initialization is not inner scope TEST_CASE(oldStylePointerCast); @@ -576,6 +577,29 @@ private: ASSERT_EQUALS("", errout.str()); } + void varScope11() + { + varScope("int f() {\n" + " int x = 0;\n" + " AB ab = { x, 0 };\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + varScope("int f() {\n" + " int x = 0;\n" + " if (a == 0) { ++x; }\n" + " AB ab = { x, 0 };\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + varScope("int f() {\n" + " int x = 0;\n" + " if (a == 0) { ++x; }\n" + " if (a == 1) { AB ab = { x, 0 }; }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void checkOldStylePointerCast(const char code[]) {