From d1b03d9c313c93dce2970c6ea0b7df1f3217f1b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 22 Oct 2013 19:37:11 +0200 Subject: [PATCH] Fixed false positive for 'object is destroyed immediately' when there is no code after the object --- lib/checkother.cpp | 2 +- test/testother.cpp | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index d04a8ae1e..80d37c72f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2427,7 +2427,7 @@ void CheckOther::checkMisusedScopedObject() const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) { if (Token::Match(tok, "[;{}] %var% (") - && Token::simpleMatch(tok->linkAt(2), ") ;") + && Token::Match(tok->linkAt(2), ") ; !!}") && symbolDatabase->isClassOrStruct(tok->next()->str()) && (!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 diff --git a/test/testother.cpp b/test/testother.cpp index 793c9c2de..689c62431 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -118,6 +118,7 @@ private: TEST_CASE(testMisusedScopeObjectDoesNotPickPureC); TEST_CASE(testMisusedScopeObjectDoesNotPickNestedClass); TEST_CASE(testMisusedScopeObjectInConstructor); + TEST_CASE(testMisusedScopeObjectNoCodeAfter); TEST_CASE(trac2071); TEST_CASE(trac2084); TEST_CASE(trac3693); @@ -3456,6 +3457,7 @@ private: "\n" " void foo() const {\n" " error();\n" + " do_something();\n" " }\n" "};\n" ); @@ -3533,6 +3535,7 @@ private: " Foo(int a, int b) { }\n" " };\n" " Foo();\n" + " do_something();\n" "}\n" ); ASSERT_EQUALS("[test.cpp:7]: (error) Instance of 'Foo' object is destroyed immediately.\n", errout.str()); @@ -3560,6 +3563,7 @@ private: "void f()\n" "{\n" " cb_watch_bool();\n" + " do_something();\n" "}\n"; check(code, "test.cpp"); @@ -3574,6 +3578,7 @@ private: "\n" "void foo() {\n" " stat(\"file.txt\", &st);\n" + " do_something();\n" "}"); ASSERT_EQUALS("",errout.str()); } @@ -3592,6 +3597,7 @@ private: "};\n" "foo::foo() {\n" " Init(0);\n" + " do_something();\n" "}\n"; check(code, "test.cpp"); @@ -3603,6 +3609,7 @@ private: "public:\n" " Foo(char x) {\n" " Foo(x, 0);\n" + " do_something();\n" " }\n" " Foo(char x, int y) { }\n" "};\n"; @@ -3610,6 +3617,14 @@ private: 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() { check("void f()\n" "{\n"