Fixed #2204 (False positive when using const class members)

This commit is contained in:
Robert Reif 2010-11-16 07:30:55 +01:00 committed by Daniel Marjamäki
parent 379daa3f24
commit 77a5d4e399
4 changed files with 78 additions and 4 deletions

View File

@ -122,6 +122,9 @@ void CheckClass::constructors()
if (var->assign || var->init || var->isStatic)
continue;
if (var->isConst && var->token->previous()->str() != "*")
continue;
// It's non-static and it's not initialized => error
if (it->type == SymbolDatabase::Func::OperatorEqual)
{

View File

@ -824,9 +824,11 @@ void SymbolDatabase::SpaceInfo::getVarList()
continue;
// Is it const..?
bool isConst = false;
if (next->str() == "const")
{
next = next->next();
isConst = true;
}
// Is it a static variable?
@ -847,6 +849,7 @@ void SymbolDatabase::SpaceInfo::getVarList()
if (next->str() == "const")
{
next = next->next();
isConst = true;
}
// Is it a variable declaration?
@ -951,7 +954,7 @@ void SymbolDatabase::SpaceInfo::getVarList()
Check::reportError(errmsg);
}
addVar(vartok, varaccess, isMutable, isStatic, isClass);
addVar(vartok, varaccess, isMutable, isStatic, isConst, isClass);
}
}
}

View File

@ -45,7 +45,7 @@ public:
class Var
{
public:
Var(const Token *token_, unsigned int index_, AccessControl access_ = Public, bool mutable_ = false, bool static_ = false, bool class_ = false)
Var(const Token *token_, unsigned int index_, AccessControl access_, bool mutable_, bool static_, bool const_, bool class_)
: token(token_),
index(index_),
assign(false),
@ -53,6 +53,7 @@ public:
access(access_),
isMutable(mutable_),
isStatic(static_),
isConst(const_),
isClass(class_)
{
}
@ -78,6 +79,9 @@ public:
/** @brief is this variable static? */
bool isStatic;
/** @brief is this variable const? */
bool isConst;
/** @brief is this variable a class (or unknown type)? */
bool isClass;
};
@ -180,9 +184,9 @@ public:
*/
void initVar(const std::string &varname);
void addVar(const Token *token_, AccessControl access_, bool mutable_, bool static_, bool class_)
void addVar(const Token *token_, AccessControl access_, bool mutable_, bool static_, bool const_, bool class_)
{
varlist.push_back(Var(token_, varlist.size(), access_, mutable_, static_, class_));
varlist.push_back(Var(token_, varlist.size(), access_, mutable_, static_, const_, class_));
}
/**

View File

@ -65,6 +65,7 @@ private:
TEST_CASE(initvar_operator_eq1); // BUG 2190376
TEST_CASE(initvar_operator_eq2); // BUG 2190376
TEST_CASE(initvar_operator_eq3);
TEST_CASE(initvar_operator_eq4); // ticket #2204
TEST_CASE(initvar_same_classname); // BUG 2208157
TEST_CASE(initvar_chained_assign); // BUG 2270433
TEST_CASE(initvar_2constructors); // BUG 2270353
@ -427,6 +428,69 @@ private:
ASSERT_EQUALS("", errout.str());
}
void initvar_operator_eq4()
{
check("class Fred\n"
"{\n"
" int i;\n"
"public:\n"
" Fred() : i(5) { }\n"
" Fred & operator=(const Fred &fred)\n"
" {\n"
" if (&fred != this)\n"
" {\n"
" }\n"
" return *this\n"
" }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
check("class Fred\n"
"{\n"
" int * i;\n"
"public:\n"
" Fred() : i(NULL) { }\n"
" Fred & operator=(const Fred &fred)\n"
" {\n"
" if (&fred != this)\n"
" {\n"
" }\n"
" return *this\n"
" }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
check("class Fred\n"
"{\n"
" const int * i;\n"
"public:\n"
" Fred() : i(NULL) { }\n"
" Fred & operator=(const Fred &fred)\n"
" {\n"
" if (&fred != this)\n"
" {\n"
" }\n"
" return *this\n"
" }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
check("class Fred\n"
"{\n"
" const int i;\n"
"public:\n"
" Fred() : i(5) { }\n"
" Fred & operator=(const Fred &fred)\n"
" {\n"
" if (&fred != this)\n"
" {\n"
" }\n"
" return *this\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void initvar_same_classname()
{
// Bug 2208157 - False positive: Uninitialized variable, same class name