Fixed #3583 (False positive Variable X is assigned a value that is never used)

This commit is contained in:
Daniel Marjamäki 2012-02-25 12:56:33 +01:00
parent 9431fb1b7e
commit bbfae8e3ae
2 changed files with 55 additions and 46 deletions

View File

@ -584,9 +584,8 @@ static const Token * skipBrackets(const Token *tok)
//---------------------------------------------------------------------------
void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const scope, Variables& variables)
{
if (scope->type == Scope::eClass || scope->type == Scope::eUnion || scope->type == Scope::eStruct)
return;
// Find declarations if the scope is executable..
if (scope->type != Scope::eClass && scope->type != Scope::eUnion && scope->type != Scope::eStruct) {
// Find declarations
for (std::list<Variable>::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) {
Variables::VariableType type = Variables::none;
@ -631,6 +630,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
} else if (Token::Match(defValTok, "( %var% )")) // Variables used to initialize the variable read.
variables.readAll(defValTok->next()->varId()); // ReadAll?
}
}
// Check variable usage
for (const Token *tok = scope->classDef->next(); tok && tok != scope->classEnd; tok = tok->next()) {

View File

@ -107,7 +107,8 @@ private:
TEST_CASE(localvararray2); // ticket #3438
TEST_CASE(localvarstring1);
TEST_CASE(localvarstring2); // ticket #2929
TEST_CASE(localvarconst);
TEST_CASE(localvarconst1);
TEST_CASE(localvarconst2);
// Don't give false positives for variables in structs/unions
TEST_CASE(localvarStruct1);
@ -2971,13 +2972,21 @@ private:
"[test.cpp:3]: (style) Unused variable: i\n", errout.str());
}
void localvarconst() {
void localvarconst1() {
functionVariableUsage("void foo() {\n"
" const bool b = true;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
}
void localvarconst2() {
functionVariableUsage("void foo() {\n"
" const int N = 10;\n"
" struct X { int x[N]; };\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
// ticket #3104 - false positive when variable is read with "if (NOT var)"
void localvarIfNOT() {
functionVariableUsage("void f() {\n"