From 7e556c215e14c3d5b6c0f1a72e5de5d9d8867a10 Mon Sep 17 00:00:00 2001 From: Lucas Manuel Rodriguez Date: Mon, 22 Jul 2013 07:25:53 +0200 Subject: [PATCH] Fixed #4861 (Improve check: object is destroyed immediately not detected when calling overloaded constructor) --- lib/checkother.cpp | 3 ++- test/testother.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index b387dcfb7..e1ec50c1e 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2295,7 +2295,8 @@ void CheckOther::checkMisusedScopedObject() if (Token::Match(tok, "[;{}] %var% (") && Token::simpleMatch(tok->linkAt(2), ") ;") && 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(); misusedScopeObjectError(tok, tok->str()); tok = tok->next(); diff --git a/test/testother.cpp b/test/testother.cpp index c0c8a7d8f..e89001c42 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -105,6 +105,7 @@ private: TEST_CASE(testMisusedScopeObjectDoesNotPickUsedObject); TEST_CASE(testMisusedScopeObjectDoesNotPickPureC); TEST_CASE(testMisusedScopeObjectDoesNotPickNestedClass); + TEST_CASE(testMisusedScopeObjectInConstructor); TEST_CASE(trac2071); TEST_CASE(trac2084); TEST_CASE(trac3693); @@ -3267,6 +3268,18 @@ private: 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() { check("void f()\n" "{\n"