From 3f72d3a877e54709d1fd1c836ec589be38b4987b Mon Sep 17 00:00:00 2001 From: Pete Johns Date: Sat, 2 Oct 2010 20:12:52 +1000 Subject: [PATCH] Check misused scope object does not pick local class method. Also fixed mistyped withinFuntion ->withinFunction. --- lib/checkother.cpp | 14 +++++++++----- test/testother.cpp | 12 ++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 453a9969d..542710d0b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3861,13 +3861,13 @@ bool CheckOther::isIdentifierObjectType(const Token * const tok) void CheckOther::checkMisusedScopedObject() { - bool withinFuntion = false; + bool withinFunction = false; unsigned int depth = 0; for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - withinFuntion |= Token::Match(tok, ") const| {"); - if (withinFuntion) + withinFunction |= Token::Match(tok, ") const| {"); + if (withinFunction) { if (tok->str() == "{") { @@ -3876,10 +3876,14 @@ void CheckOther::checkMisusedScopedObject() else if (tok->str() == "}") { --depth; - withinFuntion &= depth > 0; + withinFunction &= depth > 0; + } + else if (Token::Match(tok, "class|struct")) + { + withinFunction = false; } - if (withinFuntion && Token::Match(tok, "[;{}] %var% (") && isIdentifierObjectType(tok)) + if (withinFunction && Token::Match(tok, "[;{}] %var% (") && isIdentifierObjectType(tok)) { tok = tok->next(); misusedScopeObjectError(tok, tok->str()); diff --git a/test/testother.cpp b/test/testother.cpp index 5f8b92bfd..62c463199 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -112,6 +112,7 @@ private: TEST_CASE(testMisusedScopeObjectDoesNotPickIf); TEST_CASE(testMisusedScopeObjectDoesNotPickConstructorDeclaration); TEST_CASE(testMisusedScopeObjectDoesNotPickFunctor); + TEST_CASE(testMisusedScopeObjectDoesNotPickLocalClassMethod); } void check(const char code[]) @@ -3126,6 +3127,17 @@ private: ); ASSERT_EQUALS("", errout.str()); } + + void testMisusedScopeObjectDoesNotPickLocalClassMethod() + { + check("void f() {\n" + " class Foo {\n" + " Foo() { }\n" + " };\n" + "}\n" + ); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestOther)