Fix false positive: Misused Scope Object does not pick constructors of local class declarations.

It does pick up if there is an unused construction within the function, though.
This commit is contained in:
Pete Johns 2010-10-02 22:59:04 +10:00
parent e4c3b390cc
commit 78795dc3ac
2 changed files with 15 additions and 4 deletions

View File

@ -3863,6 +3863,7 @@ void CheckOther::checkMisusedScopedObject()
{ {
bool withinFunction = false; bool withinFunction = false;
unsigned int depth = 0; unsigned int depth = 0;
std::string className = "";
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{ {
@ -3877,10 +3878,17 @@ void CheckOther::checkMisusedScopedObject()
{ {
--depth; --depth;
withinFunction &= depth > 0; withinFunction &= depth > 0;
if (tok->strAt(1) == ";" && !className.empty())
{
isClassResults[className] = true;
className.clear();
}
} }
else if (Token::Match(tok, "class|struct")) else if (Token::Match(tok, "class|struct"))
{ {
withinFunction = false; className = tok->strAt(1);
isClassResults.insert(std::make_pair(className, false));
} }
if (withinFunction if (withinFunction

View File

@ -112,7 +112,7 @@ private:
TEST_CASE(testMisusedScopeObjectDoesNotPickIf); TEST_CASE(testMisusedScopeObjectDoesNotPickIf);
TEST_CASE(testMisusedScopeObjectDoesNotPickConstructorDeclaration); TEST_CASE(testMisusedScopeObjectDoesNotPickConstructorDeclaration);
TEST_CASE(testMisusedScopeObjectDoesNotPickFunctor); TEST_CASE(testMisusedScopeObjectDoesNotPickFunctor);
TEST_CASE(testMisusedScopeObjectDoesNotPickLocalClassMethod); TEST_CASE(testMisusedScopeObjectDoesNotPickLocalClassConstructors);
TEST_CASE(testMisusedScopeObjectDoesNotPickUsedObject); TEST_CASE(testMisusedScopeObjectDoesNotPickUsedObject);
} }
@ -3128,15 +3128,18 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void testMisusedScopeObjectDoesNotPickLocalClassMethod() void testMisusedScopeObjectDoesNotPickLocalClassConstructors()
{ {
check("void f() {\n" check("void f() {\n"
" class Foo {\n" " class Foo {\n"
" Foo() { }\n" " Foo() { }\n"
" Foo(int a) { }\n"
" Foo(int a, int b) { }\n"
" };\n" " };\n"
" Foo();\n"
"}\n" "}\n"
); );
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:7]: (error) instance of \"Foo\" object destroyed immediately\n", errout.str());
} }
void testMisusedScopeObjectDoesNotPickUsedObject() void testMisusedScopeObjectDoesNotPickUsedObject()