From 68d08b73b6cc00d0154f3df9a614517b7d27e854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 4 Jan 2009 07:49:41 +0000 Subject: [PATCH] Variable usage : Fixed a false positive for ("b = (int)a;" => a is read) --- checkother.cpp | 9 +++------ testunusedvar.cpp | 11 ++++++++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/checkother.cpp b/checkother.cpp index e7ad1b0c4..109608b83 100644 --- a/checkother.cpp +++ b/checkother.cpp @@ -816,14 +816,14 @@ static bool isOp(const Token *tok) tok->str() == ">" || tok->str() == ">=" || tok->str() == "<<" || - Token::Match(tok, "[+-*/&|,[]]"))); + Token::Match(tok, "[+-*/&|,[])]"))); } void CheckOther::functionVariableUsage() { // Parse all executing scopes.. - const Token *tok1 = Token::findmatch( _tokenizer->tokens(), ") const| {" ); - while ( tok1 ) + const Token *tok1 = _tokenizer->tokens(); + while ((tok1 = Token::findmatch( tok1->next(), ") const| {" )) != NULL) { // Varname, usage {1=declare, 2=read, 4=write} std::map varUsage; @@ -907,8 +907,5 @@ void CheckOther::functionVariableUsage() _errorLogger->reportErr(errmsg.str()); } } - - // Goto next executing scope.. - tok1 = Token::findmatch( tok1->next(), ") const| {" ); } } diff --git a/testunusedvar.cpp b/testunusedvar.cpp index 89523ef51..310505113 100644 --- a/testunusedvar.cpp +++ b/testunusedvar.cpp @@ -61,6 +61,7 @@ private: TEST_CASE( localvar2 ); TEST_CASE( localvar3 ); TEST_CASE( localvar4 ); + TEST_CASE( localvar5 ); } void structmember1() @@ -176,7 +177,15 @@ private: ASSERT_EQUALS( std::string(""), errout.str() ); } - + void localvar5() + { + functionVariableUsage( "void foo()\n" + "{\n" + " int a = 0;\n" + " b = (char)a;\n" + "}\n" ); + ASSERT_EQUALS( std::string(""), errout.str() ); + } }; REGISTER_TEST( TestUnusedVar )