Check misused scope object does not pick local class method.

Also fixed mistyped withinFuntion ->withinFunction.
This commit is contained in:
Pete Johns 2010-10-02 20:12:52 +10:00
parent 0017655f55
commit 3f72d3a877
2 changed files with 21 additions and 5 deletions

View File

@ -3861,13 +3861,13 @@ bool CheckOther::isIdentifierObjectType(const Token * const tok)
void CheckOther::checkMisusedScopedObject()
{
bool withinFuntion = false;
bool withinFunction = false;
unsigned int depth = 0;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
withinFuntion |= Token::Match(tok, ") const| {");
if (withinFuntion)
withinFunction |= Token::Match(tok, ") const| {");
if (withinFunction)
{
if (tok->str() == "{")
{
@ -3876,10 +3876,14 @@ void CheckOther::checkMisusedScopedObject()
else if (tok->str() == "}")
{
--depth;
withinFuntion &= depth > 0;
withinFunction &= depth > 0;
}
else if (Token::Match(tok, "class|struct"))
{
withinFunction = false;
}
if (withinFuntion && Token::Match(tok, "[;{}] %var% (") && isIdentifierObjectType(tok))
if (withinFunction && Token::Match(tok, "[;{}] %var% (") && isIdentifierObjectType(tok))
{
tok = tok->next();
misusedScopeObjectError(tok, tok->str());

View File

@ -112,6 +112,7 @@ private:
TEST_CASE(testMisusedScopeObjectDoesNotPickIf);
TEST_CASE(testMisusedScopeObjectDoesNotPickConstructorDeclaration);
TEST_CASE(testMisusedScopeObjectDoesNotPickFunctor);
TEST_CASE(testMisusedScopeObjectDoesNotPickLocalClassMethod);
}
void check(const char code[])
@ -3126,6 +3127,17 @@ private:
);
ASSERT_EQUALS("", errout.str());
}
void testMisusedScopeObjectDoesNotPickLocalClassMethod()
{
check("void f() {\n"
" class Foo {\n"
" Foo() { }\n"
" };\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestOther)