From 301e59cea077e1c265bd518b3597a49c2a11fb3d Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 1 Jul 2011 08:45:29 -0400 Subject: [PATCH] fix false positive for unused local class/struct variable --- lib/checkother.cpp | 9 ++++++--- test/testunusedvar.cpp | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 13a948051..250d49f51 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1753,7 +1753,7 @@ void CheckOther::functionVariableUsage() // standard type declaration with possible initialization // int i; int j = 0; static int k; if (Token::Match(tok, "[;{}] static| %type% %var% ;|=") && - (nextIsStandardType(tok) || isRecordTypeWithoutSideEffects(tok->strAt(1) == "static" ? tok->tokAt(3) : tok->tokAt(2)))) + !Token::Match(tok->next(), "return|throw")) { tok = tok->next(); @@ -1761,8 +1761,11 @@ void CheckOther::functionVariableUsage() if (isStatic) tok = tok->next(); - variables.addVar(tok->next(), Variables::standard, info, - tok->tokAt(2)->str() == "=" || isStatic); + if (tok->isStandardType() || isRecordTypeWithoutSideEffects(tok->next())) + { + variables.addVar(tok->next(), Variables::standard, info, + tok->tokAt(2)->str() == "=" || isStatic); + } tok = tok->next(); } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index ea390eeb4..b734cb0d8 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -105,6 +105,7 @@ private: TEST_CASE(localvarStruct3); TEST_CASE(localvarStruct4); // Ticket #31: sigsegv on incomplete struct TEST_CASE(localvarStruct5); + TEST_CASE(localvarStruct6); TEST_CASE(localvarOp); // Usage with arithmetic operators TEST_CASE(localvarInvert); // Usage with inverted variable @@ -2451,6 +2452,18 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvarStruct6() + { + functionVariableUsage("class Type { };\n" + "class A {\n" + "public:\n" + " Type & get() { return t; }\n" + "private:\n" + " Type t;\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + } + void localvarOp() { const char op[] = "+-*/%&|^";