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 )