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:
parent
e4c3b390cc
commit
78795dc3ac
|
@ -3863,6 +3863,7 @@ void CheckOther::checkMisusedScopedObject()
|
|||
{
|
||||
bool withinFunction = false;
|
||||
unsigned int depth = 0;
|
||||
std::string className = "";
|
||||
|
||||
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
||||
{
|
||||
|
@ -3877,10 +3878,17 @@ void CheckOther::checkMisusedScopedObject()
|
|||
{
|
||||
--depth;
|
||||
withinFunction &= depth > 0;
|
||||
|
||||
if (tok->strAt(1) == ";" && !className.empty())
|
||||
{
|
||||
isClassResults[className] = true;
|
||||
className.clear();
|
||||
}
|
||||
}
|
||||
else if (Token::Match(tok, "class|struct"))
|
||||
{
|
||||
withinFunction = false;
|
||||
className = tok->strAt(1);
|
||||
isClassResults.insert(std::make_pair(className, false));
|
||||
}
|
||||
|
||||
if (withinFunction
|
||||
|
|
|
@ -112,7 +112,7 @@ private:
|
|||
TEST_CASE(testMisusedScopeObjectDoesNotPickIf);
|
||||
TEST_CASE(testMisusedScopeObjectDoesNotPickConstructorDeclaration);
|
||||
TEST_CASE(testMisusedScopeObjectDoesNotPickFunctor);
|
||||
TEST_CASE(testMisusedScopeObjectDoesNotPickLocalClassMethod);
|
||||
TEST_CASE(testMisusedScopeObjectDoesNotPickLocalClassConstructors);
|
||||
TEST_CASE(testMisusedScopeObjectDoesNotPickUsedObject);
|
||||
}
|
||||
|
||||
|
@ -3128,15 +3128,18 @@ private:
|
|||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void testMisusedScopeObjectDoesNotPickLocalClassMethod()
|
||||
void testMisusedScopeObjectDoesNotPickLocalClassConstructors()
|
||||
{
|
||||
check("void f() {\n"
|
||||
" class Foo {\n"
|
||||
" Foo() { }\n"
|
||||
" Foo(int a) { }\n"
|
||||
" Foo(int a, int b) { }\n"
|
||||
" };\n"
|
||||
" Foo();\n"
|
||||
"}\n"
|
||||
);
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:7]: (error) instance of \"Foo\" object destroyed immediately\n", errout.str());
|
||||
}
|
||||
|
||||
void testMisusedScopeObjectDoesNotPickUsedObject()
|
||||
|
|
Loading…
Reference in New Issue