Fixed #3438 (false positive: (style) Variable 'dBuf' is not assigned a value)
This commit is contained in:
parent
fd3f384e24
commit
fd4bc12ed3
|
@ -570,6 +570,15 @@ static bool isPartOfClassStructUnion(const Token* tok)
|
|||
return false;
|
||||
}
|
||||
|
||||
// Skip [ .. ]
|
||||
static const Token * skipBrackets(const Token *tok)
|
||||
{
|
||||
while (tok && tok->str() == "[")
|
||||
tok = tok->link()->next();
|
||||
return tok;
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// Usage of function variables
|
||||
//---------------------------------------------------------------------------
|
||||
|
@ -785,13 +794,10 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
|
|||
}
|
||||
}
|
||||
|
||||
const Token *equal = tok->next();
|
||||
|
||||
if (Token::Match(tok->next(), "[ %any% ]"))
|
||||
equal = tok->tokAt(4);
|
||||
const Token *equal = skipBrackets(tok->next());
|
||||
|
||||
// checked for chained assignments
|
||||
if (tok != start && equal->str() == "=") {
|
||||
if (tok != start && equal && equal->str() == "=") {
|
||||
Variables::VariableUsage *var = variables.find(tok->varId());
|
||||
|
||||
if (var && var->_type != Variables::reference)
|
||||
|
@ -802,14 +808,14 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
|
|||
}
|
||||
|
||||
// assignment
|
||||
else if (Token::Match(tok, "%var% [") && Token::simpleMatch(tok->next()->link(), "] =")) {
|
||||
else if (Token::Match(tok, "%var% [") && Token::simpleMatch(skipBrackets(tok->next()), "=")) {
|
||||
unsigned int varid = tok->varId();
|
||||
const Variables::VariableUsage *var = variables.find(varid);
|
||||
|
||||
if (var) {
|
||||
// Consider allocating memory separately because allocating/freeing alone does not constitute using the variable
|
||||
if (var->_type == Variables::pointer &&
|
||||
Token::Match(tok->next()->link(), "] = new|malloc|calloc|g_malloc|kmalloc|vmalloc")) {
|
||||
Token::Match(skipBrackets(tok->next()), "= new|malloc|calloc|g_malloc|kmalloc|vmalloc")) {
|
||||
variables.allocateMemory(varid);
|
||||
} else if (var->_type == Variables::pointer || var->_type == Variables::reference) {
|
||||
variables.read(varid);
|
||||
|
|
|
@ -99,6 +99,7 @@ private:
|
|||
TEST_CASE(localvardynamic1);
|
||||
TEST_CASE(localvardynamic2); // ticket #2904
|
||||
TEST_CASE(localvararray1); // ticket #2780
|
||||
TEST_CASE(localvararray2); // ticket #3438
|
||||
TEST_CASE(localvarstring1);
|
||||
TEST_CASE(localvarstring2); // ticket #2929
|
||||
TEST_CASE(localvarconst);
|
||||
|
@ -2861,6 +2862,15 @@ private:
|
|||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void localvararray2() {
|
||||
functionVariableUsage("int foo() {\n"
|
||||
" int p[5][5];\n"
|
||||
" p[0][0] = 0;\n"
|
||||
" return p[0][0];\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void localvarstring1() { // ticket #1597
|
||||
functionVariableUsage("void foo() {\n"
|
||||
" std::string s;\n"
|
||||
|
|
Loading…
Reference in New Issue