Fixed #1812 (false negative: functionVariableUsage doesn't support static variables)
This commit is contained in:
parent
6fd4ba6e4f
commit
2a1edff77f
|
@ -891,6 +891,29 @@ static int doAssignment(Variables &variables, const Token *tok, bool dereference
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool nextIsStandardType(const Token *tok)
|
||||||
|
{
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() == "static")
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
return tok->isStandardType();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool nextIsStandardTypeOrVoid(const Token *tok)
|
||||||
|
{
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() == "static")
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() == "const")
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
return tok->isStandardType() || tok->str() == "void";
|
||||||
|
}
|
||||||
|
|
||||||
void CheckOther::functionVariableUsage()
|
void CheckOther::functionVariableUsage()
|
||||||
{
|
{
|
||||||
if (!_settings->_checkCodingStyle)
|
if (!_settings->_checkCodingStyle)
|
||||||
|
@ -940,72 +963,133 @@ void CheckOther::functionVariableUsage()
|
||||||
}
|
}
|
||||||
|
|
||||||
// standard type declaration with possible initialization
|
// standard type declaration with possible initialization
|
||||||
// int i; int j = 0;
|
// int i; int j = 0; static int k;
|
||||||
if (Token::Match(tok, "[;{}] %type% %var% ;|=") &&
|
if (Token::Match(tok, "[;{}] static| %type% %var% ;|=") &&
|
||||||
tok->next()->isStandardType())
|
nextIsStandardType(tok))
|
||||||
{
|
{
|
||||||
variables.addVar(tok->tokAt(2), Variables::standard,
|
tok = tok->next();
|
||||||
tok->tokAt(3)->str() == "=");
|
|
||||||
tok = tok->tokAt(2);
|
if (tok->str() == "static")
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
variables.addVar(tok->next(), Variables::standard,
|
||||||
|
tok->tokAt(2)->str() == "=" ||
|
||||||
|
tok->previous()->str() == "static");
|
||||||
|
tok = tok->next();
|
||||||
}
|
}
|
||||||
|
|
||||||
// standard type declaration and initialization using constructor
|
// standard type declaration and initialization using constructor
|
||||||
// int i(0);
|
// int i(0); static int j(0);
|
||||||
else if (Token::Match(tok, "[;{}] %type% %var% ( %any% ) ;") &&
|
else if (Token::Match(tok, "[;{}] static| %type% %var% ( %any% ) ;") &&
|
||||||
tok->next()->isStandardType())
|
nextIsStandardType(tok))
|
||||||
{
|
{
|
||||||
variables.addVar(tok->tokAt(2), Variables::standard, true);
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() == "static")
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
variables.addVar(tok->next(), Variables::standard, true);
|
||||||
|
|
||||||
// check if a local variable is used to initialize this variable
|
// check if a local variable is used to initialize this variable
|
||||||
if (tok->tokAt(4)->varId() > 0)
|
if (tok->tokAt(3)->varId() > 0)
|
||||||
variables.readAll(tok->tokAt(4)->varId());
|
variables.readAll(tok->tokAt(3)->varId());
|
||||||
tok = tok->tokAt(5);
|
tok = tok->tokAt(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
// standard type declaration of array of with possible initialization
|
// standard type declaration of array of with possible initialization
|
||||||
// int i[10]; int j[2] = { 0, 1 };
|
// int i[10]; int j[2] = { 0, 1 }; static int k[2] = { 2, 3 };
|
||||||
else if (Token::Match(tok, "[;{}] %type% %var% [ %any% ] ;|=") &&
|
else if (Token::Match(tok, "[;{}] static| %type% %var% [ %any% ] ;|=") &&
|
||||||
tok->next()->isStandardType())
|
nextIsStandardType(tok))
|
||||||
{
|
{
|
||||||
variables.addVar(tok->tokAt(2), Variables::array,
|
tok = tok->next();
|
||||||
tok->tokAt(6)->str() == "=");
|
|
||||||
|
if (tok->str() == "static")
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
variables.addVar(tok->tokAt(1), Variables::array,
|
||||||
|
tok->tokAt(5)->str() == "=" || tok->str() == "static");
|
||||||
|
|
||||||
// check for reading array size from local variable
|
// check for reading array size from local variable
|
||||||
if (tok->tokAt(4)->varId() != 0)
|
if (tok->tokAt(3)->varId() != 0)
|
||||||
variables.read(tok->tokAt(4)->varId());
|
variables.read(tok->tokAt(3)->varId());
|
||||||
|
|
||||||
// look at initializers
|
// look at initializers
|
||||||
if (Token::Match(tok->tokAt(6), "= {"))
|
if (Token::Match(tok->tokAt(5), "= {"))
|
||||||
{
|
{
|
||||||
tok = tok->tokAt(8);
|
tok = tok->tokAt(7);
|
||||||
while (tok && tok->str() != "}")
|
while (tok->str() != "}")
|
||||||
{
|
{
|
||||||
if (Token::Match(tok, "%var%"))
|
if (Token::Match(tok, "%var%"))
|
||||||
variables.read(tok->varId());
|
variables.read(tok->varId());
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
}
|
}
|
||||||
tok = tok->next();
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
tok = tok->tokAt(5);
|
tok = tok->tokAt(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
// pointer or reference declaration with possible initialization
|
// pointer or reference declaration with possible initialization
|
||||||
// int * i; int * j = 0;
|
// int * i; int * j = 0; static int * k = 0;
|
||||||
else if (Token::Match(tok, "[;{}] %type% *|& %var% ;|="))
|
else if (Token::Match(tok, "[;{}] static| const| %type% *|& %var% ;|="))
|
||||||
{
|
{
|
||||||
if (tok->next()->str() != "return")
|
bool isStatic = false;
|
||||||
|
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() == "static")
|
||||||
|
{
|
||||||
|
tok = tok->next();
|
||||||
|
isStatic = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tok->str() == "const")
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() != "return")
|
||||||
{
|
{
|
||||||
Variables::VariableType type;
|
Variables::VariableType type;
|
||||||
|
|
||||||
if (tok->tokAt(2)->str() == "*")
|
if (tok->next()->str() == "*")
|
||||||
type = Variables::pointer;
|
type = Variables::pointer;
|
||||||
else
|
else
|
||||||
type = Variables::reference;
|
type = Variables::reference;
|
||||||
|
|
||||||
|
bool written = tok->tokAt(3)->str() == "=";
|
||||||
|
|
||||||
|
variables.addVar(tok->tokAt(2), type, written || isStatic);
|
||||||
|
|
||||||
|
int offset = 0;
|
||||||
|
|
||||||
|
// check for assignment
|
||||||
|
if (written)
|
||||||
|
offset = doAssignment(variables, tok->tokAt(2), false);
|
||||||
|
|
||||||
|
tok = tok->tokAt(2 + offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pointer to pointer declaration with possible initialization
|
||||||
|
// int ** i; int ** j = 0; static int ** k = 0;
|
||||||
|
else if (Token::Match(tok, "[;{}] static| const| %type% * * %var% ;|="))
|
||||||
|
{
|
||||||
|
bool isStatic = false;
|
||||||
|
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() == "static")
|
||||||
|
{
|
||||||
|
tok = tok->next();
|
||||||
|
isStatic = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tok->str() == "const")
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() != "return")
|
||||||
|
{
|
||||||
bool written = tok->tokAt(4)->str() == "=";
|
bool written = tok->tokAt(4)->str() == "=";
|
||||||
|
|
||||||
variables.addVar(tok->tokAt(3), type, written);
|
variables.addVar(tok->tokAt(3), Variables::pointerPointer, written || isStatic);
|
||||||
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
|
|
||||||
|
@ -1017,57 +1101,21 @@ void CheckOther::functionVariableUsage()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// const pointer or reference declaration with possible initialization
|
|
||||||
// const int * i; const int * j = 0;
|
|
||||||
else if (Token::Match(tok, "[;{}] const %type% *|& %var% ;|="))
|
|
||||||
{
|
|
||||||
Variables::VariableType type;
|
|
||||||
|
|
||||||
if (tok->tokAt(3)->str() == "*")
|
|
||||||
type = Variables::pointer;
|
|
||||||
else
|
|
||||||
type = Variables::reference;
|
|
||||||
|
|
||||||
bool written = tok->tokAt(5)->str() == "=";
|
|
||||||
|
|
||||||
variables.addVar(tok->tokAt(4), type, written);
|
|
||||||
|
|
||||||
int offset = 0;
|
|
||||||
|
|
||||||
// check for assignment
|
|
||||||
if (written)
|
|
||||||
offset = doAssignment(variables, tok->tokAt(4), false);
|
|
||||||
|
|
||||||
tok = tok->tokAt(4 + offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
// pointer to pointer declaration with possible initialization
|
|
||||||
// int ** i; int ** j = 0;
|
|
||||||
else if (Token::Match(tok, "[;{}] %type% * * %var% ;|="))
|
|
||||||
{
|
|
||||||
if (tok->next()->str() != "return")
|
|
||||||
{
|
|
||||||
bool written = tok->tokAt(5)->str() == "=";
|
|
||||||
|
|
||||||
variables.addVar(tok->tokAt(4), Variables::pointerPointer, written);
|
|
||||||
|
|
||||||
int offset = 0;
|
|
||||||
|
|
||||||
// check for assignment
|
|
||||||
if (written)
|
|
||||||
offset = doAssignment(variables, tok->tokAt(4), false);
|
|
||||||
|
|
||||||
tok = tok->tokAt(4 + offset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// pointer or reference of struct or union declaration with possible initialization
|
// pointer or reference of struct or union declaration with possible initialization
|
||||||
// struct s * i; struct s * j = 0;
|
// struct s * i; struct s * j = 0; static struct s * k = 0;
|
||||||
else if (Token::Match(tok, "[;{}] const| struct|union %type% *|& %var% ;|="))
|
else if (Token::Match(tok, "[;{}] static| const| struct|union %type% *|& %var% ;|="))
|
||||||
{
|
{
|
||||||
Variables::VariableType type;
|
Variables::VariableType type;
|
||||||
|
bool isStatic = false;
|
||||||
|
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() == "static")
|
||||||
|
{
|
||||||
|
tok = tok->next();
|
||||||
|
isStatic = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (tok->str() == "const")
|
if (tok->str() == "const")
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
|
|
||||||
|
@ -1078,7 +1126,7 @@ void CheckOther::functionVariableUsage()
|
||||||
|
|
||||||
const bool written = tok->strAt(4) == "=";
|
const bool written = tok->strAt(4) == "=";
|
||||||
|
|
||||||
variables.addVar(tok->tokAt(3), type, written);
|
variables.addVar(tok->tokAt(3), type, written || isStatic);
|
||||||
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
|
|
||||||
|
@ -1090,13 +1138,21 @@ void CheckOther::functionVariableUsage()
|
||||||
}
|
}
|
||||||
|
|
||||||
// pointer or reference declaration with initialization using constructor
|
// pointer or reference declaration with initialization using constructor
|
||||||
// int * i(j); int * k(i);
|
// int * i(j); int * k(i); static int * l(i);
|
||||||
else if (Token::Match(tok, "[;{}] %type% &|* %var% ( %any% ) ;") &&
|
else if (Token::Match(tok, "[;{}] static| const| %type% &|* %var% ( %any% ) ;") &&
|
||||||
(tok->next()->isStandardType() || tok->next()->str() == "void"))
|
nextIsStandardTypeOrVoid(tok))
|
||||||
{
|
{
|
||||||
Variables::VariableType type;
|
Variables::VariableType type;
|
||||||
|
|
||||||
if (tok->tokAt(2)->str() == "*")
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() == "static")
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() == "const")
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->next()->str() == "*")
|
||||||
type = Variables::pointer;
|
type = Variables::pointer;
|
||||||
else
|
else
|
||||||
type = Variables::reference;
|
type = Variables::reference;
|
||||||
|
@ -1104,10 +1160,10 @@ void CheckOther::functionVariableUsage()
|
||||||
unsigned int varid = 0;
|
unsigned int varid = 0;
|
||||||
|
|
||||||
// check for aliased variable
|
// check for aliased variable
|
||||||
if (Token::Match(tok->tokAt(5), "%var%"))
|
if (Token::Match(tok->tokAt(4), "%var%"))
|
||||||
varid = tok->tokAt(5)->varId();
|
varid = tok->tokAt(4)->varId();
|
||||||
|
|
||||||
variables.addVar(tok->tokAt(3), type, true);
|
variables.addVar(tok->tokAt(2), type, true);
|
||||||
|
|
||||||
// check if a local variable is used to initialize this variable
|
// check if a local variable is used to initialize this variable
|
||||||
if (varid > 0)
|
if (varid > 0)
|
||||||
|
@ -1116,7 +1172,7 @@ void CheckOther::functionVariableUsage()
|
||||||
|
|
||||||
if (type == Variables::pointer)
|
if (type == Variables::pointer)
|
||||||
{
|
{
|
||||||
variables.use(tok->tokAt(5)->varId());
|
variables.use(tok->tokAt(4)->varId());
|
||||||
|
|
||||||
if (var && (var->_type == Variables::array ||
|
if (var && (var->_type == Variables::array ||
|
||||||
var->_type == Variables::pointer))
|
var->_type == Variables::pointer))
|
||||||
|
@ -1124,66 +1180,65 @@ void CheckOther::functionVariableUsage()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
variables.readAll(tok->tokAt(5)->varId());
|
variables.readAll(tok->tokAt(4)->varId());
|
||||||
if (var)
|
if (var)
|
||||||
var->_aliases.insert(tok->varId());
|
var->_aliases.insert(tok->varId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tok = tok->tokAt(6);
|
tok = tok->tokAt(5);
|
||||||
}
|
|
||||||
|
|
||||||
// const pointer or reference declaration with initialization using constructor
|
|
||||||
// const int * i(j); const int * k(i);
|
|
||||||
else if (Token::Match(tok, "[;{}] const %type% &|* %var% ( %any% ) ;") &&
|
|
||||||
(tok->tokAt(2)->isStandardType() || tok->tokAt(2)->str() == "void"))
|
|
||||||
{
|
|
||||||
Variables::VariableType type;
|
|
||||||
|
|
||||||
if (tok->tokAt(3)->str() == "*")
|
|
||||||
type = Variables::pointer;
|
|
||||||
else
|
|
||||||
type = Variables::reference;
|
|
||||||
|
|
||||||
unsigned int varid = 0;
|
|
||||||
|
|
||||||
// check for aliased variable
|
|
||||||
if (Token::Match(tok->tokAt(6), "%var%"))
|
|
||||||
varid = tok->tokAt(6)->varId();
|
|
||||||
|
|
||||||
variables.addVar(tok->tokAt(4), type, true);
|
|
||||||
|
|
||||||
// check if a local variable is used to initialize this variable
|
|
||||||
if (varid > 0)
|
|
||||||
{
|
|
||||||
Variables::VariableUsage *var = variables.find(varid);
|
|
||||||
|
|
||||||
if (type == Variables::pointer)
|
|
||||||
{
|
|
||||||
variables.use(tok->tokAt(6)->varId());
|
|
||||||
|
|
||||||
if (var && (var->_type == Variables::array ||
|
|
||||||
var->_type == Variables::pointer))
|
|
||||||
var->_aliases.insert(tok->varId());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
variables.readAll(tok->tokAt(6)->varId());
|
|
||||||
if (var)
|
|
||||||
var->_aliases.insert(tok->varId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tok = tok->tokAt(7);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// array of pointer or reference declaration with possible initialization
|
// array of pointer or reference declaration with possible initialization
|
||||||
// int * p[10]; int * q[10] = { 0 };
|
// int * p[10]; int * q[10] = { 0 }; static int * * r[10] = { 0 };
|
||||||
else if (Token::Match(tok, "[;{}] %type% *|& %var% [ %any% ] ;|="))
|
else if (Token::Match(tok, "[;{}] static| const| %type% *|& %var% [ %any% ] ;|="))
|
||||||
{
|
{
|
||||||
if (tok->next()->str() != "return")
|
bool isStatic = false;
|
||||||
|
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() == "static")
|
||||||
{
|
{
|
||||||
|
tok = tok->next();
|
||||||
|
isStatic = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tok->str() == "const")
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() != "return")
|
||||||
|
{
|
||||||
|
variables.addVar(tok->tokAt(2),
|
||||||
|
tok->next()->str() == "*" ? Variables::pointerArray : Variables::referenceArray,
|
||||||
|
tok->tokAt(6)->str() == "=" || isStatic);
|
||||||
|
|
||||||
|
// check for reading array size from local variable
|
||||||
|
if (tok->tokAt(4)->varId() != 0)
|
||||||
|
variables.read(tok->tokAt(4)->varId());
|
||||||
|
|
||||||
|
tok = tok->tokAt(5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// array of pointer or reference of struct or union declaration with possible initialization
|
||||||
|
// struct S * p[10]; struct T * q[10] = { 0 }; static struct S * r[10] = { 0 };
|
||||||
|
else if (Token::Match(tok, "[;{}] static| const| struct|union %type% *|& %var% [ %any% ] ;|="))
|
||||||
|
{
|
||||||
|
bool isStatic = false;
|
||||||
|
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
|
if (tok->str() == "static")
|
||||||
|
{
|
||||||
|
tok = tok->next();
|
||||||
|
isStatic = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tok->str() == "const")
|
||||||
|
tok = tok->next();
|
||||||
|
|
||||||
variables.addVar(tok->tokAt(3),
|
variables.addVar(tok->tokAt(3),
|
||||||
tok->tokAt(2)->str() == "*" ? Variables::pointerArray : Variables::referenceArray,
|
tok->tokAt(2)->str() == "*" ? Variables::pointerArray : Variables::referenceArray,
|
||||||
tok->tokAt(7)->str() == "=");
|
tok->tokAt(7)->str() == "=" || isStatic);
|
||||||
|
|
||||||
// check for reading array size from local variable
|
// check for reading array size from local variable
|
||||||
if (tok->tokAt(5)->varId() != 0)
|
if (tok->tokAt(5)->varId() != 0)
|
||||||
|
@ -1191,40 +1246,6 @@ void CheckOther::functionVariableUsage()
|
||||||
|
|
||||||
tok = tok->tokAt(6);
|
tok = tok->tokAt(6);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// const array of pointer or reference declaration with possible initialization
|
|
||||||
// const int * p[10]; const int * q[10] = { 0 };
|
|
||||||
else if (Token::Match(tok, "[;{}] const %type% *|& %var% [ %any% ] ;|="))
|
|
||||||
{
|
|
||||||
variables.addVar(tok->tokAt(4),
|
|
||||||
tok->tokAt(3)->str() == "*" ? Variables::pointerArray : Variables::referenceArray,
|
|
||||||
tok->tokAt(8)->str() == "=");
|
|
||||||
|
|
||||||
// check for reading array size from local variable
|
|
||||||
if (tok->tokAt(6)->varId() != 0)
|
|
||||||
variables.read(tok->tokAt(6)->varId());
|
|
||||||
|
|
||||||
tok = tok->tokAt(7);
|
|
||||||
}
|
|
||||||
|
|
||||||
// array of pointer or reference of struct or union declaration with possible initialization
|
|
||||||
// struct S * p[10]; struct T * q[10] = { 0 };
|
|
||||||
else if (Token::Match(tok, "[;{}] const| struct|union %type% *|& %var% [ %any% ] ;|="))
|
|
||||||
{
|
|
||||||
if (tok->next()->str() == "const")
|
|
||||||
tok = tok->next();
|
|
||||||
|
|
||||||
variables.addVar(tok->tokAt(4),
|
|
||||||
tok->tokAt(3)->str() == "*" ? Variables::pointerArray : Variables::referenceArray,
|
|
||||||
tok->tokAt(8)->str() == "=");
|
|
||||||
|
|
||||||
// check for reading array size from local variable
|
|
||||||
if (tok->tokAt(6)->varId() != 0)
|
|
||||||
variables.read(tok->tokAt(6)->varId());
|
|
||||||
|
|
||||||
tok = tok->tokAt(7);
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (Token::Match(tok, "delete|return|throw %var%"))
|
else if (Token::Match(tok, "delete|return|throw %var%"))
|
||||||
variables.readAll(tok->next()->varId());
|
variables.readAll(tok->next()->varId());
|
||||||
|
|
|
@ -75,6 +75,7 @@ private:
|
||||||
TEST_CASE(localvaralias6); // ticket #1729
|
TEST_CASE(localvaralias6); // ticket #1729
|
||||||
TEST_CASE(localvaralias7); // ticket #1732
|
TEST_CASE(localvaralias7); // ticket #1732
|
||||||
TEST_CASE(localvarasm);
|
TEST_CASE(localvarasm);
|
||||||
|
TEST_CASE(localvarstatic);
|
||||||
|
|
||||||
// Don't give false positives for variables in structs/unions
|
// Don't give false positives for variables in structs/unions
|
||||||
TEST_CASE(localvarStruct1);
|
TEST_CASE(localvarStruct1);
|
||||||
|
@ -1970,6 +1971,50 @@ private:
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void localvarstatic()
|
||||||
|
{
|
||||||
|
functionVariableUsage("void foo()\n"
|
||||||
|
"{\n"
|
||||||
|
" static int i;\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("void foo()\n"
|
||||||
|
"{\n"
|
||||||
|
" static int i = 0;\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("void foo()\n"
|
||||||
|
"{\n"
|
||||||
|
" static int i(0);\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("void foo()\n"
|
||||||
|
"{\n"
|
||||||
|
" static int i(a);\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("void foo()\n"
|
||||||
|
"{\n"
|
||||||
|
" static int j = 0;\n"
|
||||||
|
" static int i(j);\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("int * foo(int x)\n"
|
||||||
|
"{\n"
|
||||||
|
" static int a[] = { 3, 4, 5, 6 };\n"
|
||||||
|
" static int b[] = { 4, 5, 6, 7 };\n"
|
||||||
|
" static int c[] = { 5, 6, 7, 8 };\n"
|
||||||
|
" b[1] = 1;\n"
|
||||||
|
" return x ? a : c;\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
REGISTER_TEST(TestUnusedVar)
|
REGISTER_TEST(TestUnusedVar)
|
||||||
|
|
Loading…
Reference in New Issue