From 78795dc3acb1cdaebccf83f15605807c30b31746 Mon Sep 17 00:00:00 2001 From: Pete Johns Date: Sat, 2 Oct 2010 22:59:04 +1000 Subject: [PATCH] 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. --- lib/checkother.cpp | 10 +++++++++- test/testother.cpp | 9 ++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 17ce079d6..007a6f136 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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 diff --git a/test/testother.cpp b/test/testother.cpp index 4e52b0487..e92fed815 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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()