From dcd64a7ce68ecd5438b1ead2a963126d9c1673f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 4 Jan 2009 17:35:19 +0000 Subject: [PATCH] Variable usage : Fixed false positives for struct/union member variables --- checkother.cpp | 3 ++- testunusedvar.cpp | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/checkother.cpp b/checkother.cpp index 8575389bd..7fd1a7a5c 100644 --- a/checkother.cpp +++ b/checkother.cpp @@ -838,7 +838,8 @@ void CheckOther::functionVariableUsage() if ( indentlevel <= 0 ) break; } - else if ( Token::Match(tok, "struct|union|class {") ) + else if ( Token::Match(tok, "struct|union|class {") || + Token::Match(tok, "struct|union|class %type% {") ) { while ( tok && tok->str() != "}" ) tok = tok->next(); diff --git a/testunusedvar.cpp b/testunusedvar.cpp index f769f2b90..85042fd25 100644 --- a/testunusedvar.cpp +++ b/testunusedvar.cpp @@ -62,7 +62,10 @@ private: TEST_CASE( localvar3 ); TEST_CASE( localvar4 ); TEST_CASE( localvar5 ); - TEST_CASE( localvar6 ); + + // Don't give false positives for variables in structs/unions + TEST_CASE( localvarStruct1 ); + TEST_CASE( localvarStruct2 ); TEST_CASE( localvarMod ); // Usage with modulo TEST_CASE( localvarInvert ); // Usage with inverted variable @@ -193,7 +196,9 @@ private: ASSERT_EQUALS( std::string(""), errout.str() ); } - void localvar6() + + + void localvarStruct1() { functionVariableUsage( "void foo()\n" "{\n" @@ -203,6 +208,18 @@ private: ASSERT_EQUALS( std::string(""), errout.str() ); } + void localvarStruct2() + { + functionVariableUsage( "void foo()\n" + "{\n" + " struct ABC { int a, b, c; };\n" + " struct ABC abc = { 1, 2, 3 };\n" + "}\n" ); + ASSERT_EQUALS( std::string(""), errout.str() ); + } + + + void localvarMod() { functionVariableUsage( "int main()\n"