Fixed false positive for 'object is destroyed immediately' when there is no code after the object

This commit is contained in:
Daniel Marjamäki 2013-10-22 19:37:11 +02:00
parent a55056c770
commit d1b03d9c31
2 changed files with 16 additions and 1 deletions

View File

@ -2427,7 +2427,7 @@ void CheckOther::checkMisusedScopedObject()
const Scope * scope = symbolDatabase->functionScopes[i]; const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) { for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
if (Token::Match(tok, "[;{}] %var% (") if (Token::Match(tok, "[;{}] %var% (")
&& Token::simpleMatch(tok->linkAt(2), ") ;") && Token::Match(tok->linkAt(2), ") ; !!}")
&& symbolDatabase->isClassOrStruct(tok->next()->str()) && symbolDatabase->isClassOrStruct(tok->next()->str())
&& (!tok->next()->function() || // is not a function on this scope && (!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->next()->function() && tok->next()->function()->isConstructor()))) { // or is function in this scope and it's a ctor

View File

@ -118,6 +118,7 @@ private:
TEST_CASE(testMisusedScopeObjectDoesNotPickPureC); TEST_CASE(testMisusedScopeObjectDoesNotPickPureC);
TEST_CASE(testMisusedScopeObjectDoesNotPickNestedClass); TEST_CASE(testMisusedScopeObjectDoesNotPickNestedClass);
TEST_CASE(testMisusedScopeObjectInConstructor); TEST_CASE(testMisusedScopeObjectInConstructor);
TEST_CASE(testMisusedScopeObjectNoCodeAfter);
TEST_CASE(trac2071); TEST_CASE(trac2071);
TEST_CASE(trac2084); TEST_CASE(trac2084);
TEST_CASE(trac3693); TEST_CASE(trac3693);
@ -3456,6 +3457,7 @@ private:
"\n" "\n"
" void foo() const {\n" " void foo() const {\n"
" error();\n" " error();\n"
" do_something();\n"
" }\n" " }\n"
"};\n" "};\n"
); );
@ -3533,6 +3535,7 @@ private:
" Foo(int a, int b) { }\n" " Foo(int a, int b) { }\n"
" };\n" " };\n"
" Foo();\n" " Foo();\n"
" do_something();\n"
"}\n" "}\n"
); );
ASSERT_EQUALS("[test.cpp:7]: (error) Instance of 'Foo' object is destroyed immediately.\n", errout.str()); ASSERT_EQUALS("[test.cpp:7]: (error) Instance of 'Foo' object is destroyed immediately.\n", errout.str());
@ -3560,6 +3563,7 @@ private:
"void f()\n" "void f()\n"
"{\n" "{\n"
" cb_watch_bool();\n" " cb_watch_bool();\n"
" do_something();\n"
"}\n"; "}\n";
check(code, "test.cpp"); check(code, "test.cpp");
@ -3574,6 +3578,7 @@ private:
"\n" "\n"
"void foo() {\n" "void foo() {\n"
" stat(\"file.txt\", &st);\n" " stat(\"file.txt\", &st);\n"
" do_something();\n"
"}"); "}");
ASSERT_EQUALS("",errout.str()); ASSERT_EQUALS("",errout.str());
} }
@ -3592,6 +3597,7 @@ private:
"};\n" "};\n"
"foo::foo() {\n" "foo::foo() {\n"
" Init(0);\n" " Init(0);\n"
" do_something();\n"
"}\n"; "}\n";
check(code, "test.cpp"); check(code, "test.cpp");
@ -3603,6 +3609,7 @@ private:
"public:\n" "public:\n"
" Foo(char x) {\n" " Foo(char x) {\n"
" Foo(x, 0);\n" " Foo(x, 0);\n"
" do_something();\n"
" }\n" " }\n"
" Foo(char x, int y) { }\n" " Foo(char x, int y) { }\n"
"};\n"; "};\n";
@ -3610,6 +3617,14 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (error) Instance of 'Foo' object is destroyed immediately.\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Instance of 'Foo' object is destroyed immediately.\n", errout.str());
} }
void testMisusedScopeObjectNoCodeAfter() {
check("class Foo {};\n"
"void f() {\n"
" Foo();\n" // No code after class => don't warn
"}", "test.cpp");
ASSERT_EQUALS("", errout.str());
}
void trac2084() { void trac2084() {
check("void f()\n" check("void f()\n"
"{\n" "{\n"