Java: fixed false positives about uninitialized variable

This commit is contained in:
Daniel Marjamäki 2010-10-23 10:56:30 +02:00
parent 5deb046ac5
commit 120073f000
2 changed files with 11 additions and 1 deletions

View File

@ -743,7 +743,7 @@ void CheckClass::SpaceInfo::getVarList()
} }
// If the vartok was set in the if-blocks above, create a entry for this variable.. // If the vartok was set in the if-blocks above, create a entry for this variable..
if (vartok && vartok->str() != "operator") if (vartok && vartok->str() != "operator" && !Token::Match(vartok->next(), "; %varid% =", vartok->varId()))
{ {
if (vartok->varId() == 0 && check->_settings->debugwarnings) if (vartok->varId() == 0 && check->_settings->debugwarnings)
{ {

View File

@ -81,6 +81,7 @@ private:
TEST_CASE(uninitFunction3); // No FP when initialized in function TEST_CASE(uninitFunction3); // No FP when initialized in function
TEST_CASE(uninitSameClassName); // No FP when two classes have the same name TEST_CASE(uninitSameClassName); // No FP when two classes have the same name
TEST_CASE(uninitFunctionOverload); // No FP when there are overloaded functions TEST_CASE(uninitFunctionOverload); // No FP when there are overloaded functions
TEST_CASE(uninitJava); // Java: no FP when variable is initialized in declaration
TEST_CASE(noConstructor1); TEST_CASE(noConstructor1);
TEST_CASE(noConstructor2); TEST_CASE(noConstructor2);
@ -2228,6 +2229,15 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void uninitJava()
{
checkUninitVar("class A {\n"
" private: int i = 0;\n"
" public: A() { }\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void checkNoConstructor(const char code[]) void checkNoConstructor(const char code[])
{ {