Fixed false-positive: Object is referenced on construction

struct Foo {
        void bar() {
        }
    };

    void fn() {
        Foo().bar(); // This caused a false-positive
    }
This commit is contained in:
Pete Johns 2010-10-02 20:26:29 +10:00
parent 3f72d3a877
commit b72b699b76
2 changed files with 20 additions and 1 deletions

View File

@ -3883,7 +3883,11 @@ void CheckOther::checkMisusedScopedObject()
withinFunction = false; withinFunction = false;
} }
if (withinFunction && Token::Match(tok, "[;{}] %var% (") && isIdentifierObjectType(tok)) if (withinFunction
&& Token::Match(tok, "[;{}] %var% (")
&& isIdentifierObjectType(tok)
&& !Token::Match(tok->tokAt(2)->link(), ") .")
)
{ {
tok = tok->next(); tok = tok->next();
misusedScopeObjectError(tok, tok->str()); misusedScopeObjectError(tok, tok->str());

View File

@ -113,6 +113,7 @@ private:
TEST_CASE(testMisusedScopeObjectDoesNotPickConstructorDeclaration); TEST_CASE(testMisusedScopeObjectDoesNotPickConstructorDeclaration);
TEST_CASE(testMisusedScopeObjectDoesNotPickFunctor); TEST_CASE(testMisusedScopeObjectDoesNotPickFunctor);
TEST_CASE(testMisusedScopeObjectDoesNotPickLocalClassMethod); TEST_CASE(testMisusedScopeObjectDoesNotPickLocalClassMethod);
TEST_CASE(testMisusedScopeObjectDoesNotPickUsedObject);
} }
void check(const char code[]) void check(const char code[])
@ -3138,6 +3139,20 @@ private:
); );
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void testMisusedScopeObjectDoesNotPickUsedObject()
{
check("struct Foo {\n"
" void bar() {\n"
" }\n"
"};\n"
"\n"
"void fn() {\n"
" Foo().bar();\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
}
}; };
REGISTER_TEST(TestOther) REGISTER_TEST(TestOther)