From 101afe4344459c216ca1e21d68954bc242963df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 17 Mar 2008 11:05:30 +0000 Subject: [PATCH] Checking variable scope --- CheckOther.cpp | 9 +++++++-- tests.cpp | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CheckOther.cpp b/CheckOther.cpp index 19b2cfd58..bfc87bec3 100644 --- a/CheckOther.cpp +++ b/CheckOther.cpp @@ -579,7 +579,7 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[ tok = tok->next; // Check if the variable is used in this indentlevel.. - bool used = false; + bool used = false, used1 = false; int indentlevel = 0; while ( indentlevel >= 0 && tok ) { @@ -591,11 +591,16 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[ else if ( tok->str[0] == '}' ) { indentlevel--; + if ( indentlevel == 0 ) + { + used1 = used; + used = false; + } } else if ( strcmp(tok->str, varname) == 0 ) { - if ( indentlevel == 0 ) + if ( indentlevel == 0 || used1 ) return; used = true; } diff --git a/tests.cpp b/tests.cpp index f30a825fe..3617412e6 100644 --- a/tests.cpp +++ b/tests.cpp @@ -752,5 +752,19 @@ static void unused_variable() "}\n"; check( CheckVariableScope, __LINE__, test4, "" ); + + + const char test5[] = "static void f()\n" + "{\n" + " int i = 0;\n" + " {\n" + " i+5;\n" + " }\n" + " {\n" + " i+5;\n" + " }\n" + "}\n"; + check( CheckVariableScope, __LINE__, test5, "" ); + }