Fixed #2639 (False positive: confusion between struct and function)
This commit is contained in:
parent
27f4b8b88b
commit
ed11a9e3cb
|
@ -2941,6 +2941,23 @@ void CheckOther::checkMathFunctions()
|
|||
}
|
||||
}
|
||||
|
||||
/** Is there a function with given name? */
|
||||
static bool isFunction(const std::string &name, const Token *startToken)
|
||||
{
|
||||
const std::string pattern1(name + " (");
|
||||
for (const Token *tok = startToken; tok; tok = tok->next())
|
||||
{
|
||||
// skip executable scopes etc
|
||||
if (tok->str() == "(" || tok->str() == "{")
|
||||
tok = tok->link();
|
||||
|
||||
// function declaration/implementation found
|
||||
if (Token::simpleMatch(tok, pattern1.c_str()))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CheckOther::checkMisusedScopedObject()
|
||||
{
|
||||
// Skip this check for .c files
|
||||
|
@ -2979,7 +2996,7 @@ void CheckOther::checkMisusedScopedObject()
|
|||
if (Token::Match(tok, "[;{}] %var% (")
|
||||
&& Token::simpleMatch(tok->tokAt(2)->link(), ") ;")
|
||||
&& symbolDatabase->isClassOrStruct(tok->next()->str())
|
||||
)
|
||||
&& !isFunction(tok->next()->str(), _tokenizer->tokens()))
|
||||
{
|
||||
tok = tok->next();
|
||||
misusedScopeObjectError(tok, tok->str());
|
||||
|
|
|
@ -1774,6 +1774,15 @@ private:
|
|||
|
||||
check(code, "test.c");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
// Ticket #2639
|
||||
check("struct stat { int a; int b; };\n"
|
||||
"void stat(const char *fn, struct stat *);\n"
|
||||
"\n"
|
||||
"void foo() {\n"
|
||||
" stat(\"file.txt\", &st);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("",errout.str());
|
||||
}
|
||||
|
||||
void testMisusedScopeObjectDoesNotPickNestedClass()
|
||||
|
|
Loading…
Reference in New Issue