Fixed #4861 (Improve check: object is destroyed immediately not detected when calling overloaded constructor)

This commit is contained in:
Lucas Manuel Rodriguez 2013-07-22 07:25:53 +02:00 committed by Daniel Marjamäki
parent 63cba99d7e
commit 7e556c215e
2 changed files with 15 additions and 1 deletions

View File

@ -2295,7 +2295,8 @@ void CheckOther::checkMisusedScopedObject()
if (Token::Match(tok, "[;{}] %var% (") if (Token::Match(tok, "[;{}] %var% (")
&& Token::simpleMatch(tok->linkAt(2), ") ;") && Token::simpleMatch(tok->linkAt(2), ") ;")
&& symbolDatabase->isClassOrStruct(tok->next()->str()) && symbolDatabase->isClassOrStruct(tok->next()->str())
&& !tok->next()->function()) { && (!tok->next()->function() || // is not a function on this scope
(tok->next()->function() && tok->next()->function()->isConstructor()))) { // or is function in this scope and it's a ctor
tok = tok->next(); tok = tok->next();
misusedScopeObjectError(tok, tok->str()); misusedScopeObjectError(tok, tok->str());
tok = tok->next(); tok = tok->next();

View File

@ -105,6 +105,7 @@ private:
TEST_CASE(testMisusedScopeObjectDoesNotPickUsedObject); TEST_CASE(testMisusedScopeObjectDoesNotPickUsedObject);
TEST_CASE(testMisusedScopeObjectDoesNotPickPureC); TEST_CASE(testMisusedScopeObjectDoesNotPickPureC);
TEST_CASE(testMisusedScopeObjectDoesNotPickNestedClass); TEST_CASE(testMisusedScopeObjectDoesNotPickNestedClass);
TEST_CASE(testMisusedScopeObjectInConstructor);
TEST_CASE(trac2071); TEST_CASE(trac2071);
TEST_CASE(trac2084); TEST_CASE(trac2084);
TEST_CASE(trac3693); TEST_CASE(trac3693);
@ -3267,6 +3268,18 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void testMisusedScopeObjectInConstructor() {
const char code[] = "class Foo {\n"
"public:\n"
" Foo(char x) {\n"
" Foo(x, 0);\n"
" }\n"
" Foo(char x, int y) { }\n"
"};\n";
check(code, "test.cpp");
ASSERT_EQUALS("[test.cpp:4]: (error) Instance of 'Foo' object is destroyed immediately.\n", errout.str());
}
void trac2084() { void trac2084() {
check("void f()\n" check("void f()\n"
"{\n" "{\n"