add support for unused const local variables

This commit is contained in:
Robert Reif 2011-07-24 12:09:59 -04:00
parent 4e6800c474
commit 9a4447c835
2 changed files with 21 additions and 0 deletions

View File

@ -1888,6 +1888,18 @@ void CheckOther::functionVariableUsage()
tok = tok->next();
}
// standard const type declaration
// const int i = x;
if (Token::Match(tok, "[;{}] const %type% %var% ="))
{
tok = tok->next()->next();
if (tok->isStandardType() || isRecordTypeWithoutSideEffects(tok->next()))
variables.addVar(tok->next(), Variables::standard, info, true);
tok = tok->next();
}
// std::string declaration with possible initialization
// std::string s; std::string s = "string";
else if (Token::Match(tok, "[;{}] static| std :: string %var% ;|="))

View File

@ -101,6 +101,7 @@ private:
TEST_CASE(localvararray1); // ticket #2780
TEST_CASE(localvarstring1);
TEST_CASE(localvarstring2); // ticket #2929
TEST_CASE(localvarconst);
// Don't give false positives for variables in structs/unions
TEST_CASE(localvarStruct1);
@ -2930,6 +2931,14 @@ private:
ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: s\n"
"[test.cpp:3]: (style) Unused variable: i\n", errout.str());
}
void localvarconst()
{
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());
}
};
REGISTER_TEST(TestUnusedVar)