Fix #895 (false positive: Suspicious pointer subtraction)

http://sourceforge.net/apps/trac/cppcheck/ticket/895
This commit is contained in:
Reijo Tomperi 2009-11-04 21:36:27 +02:00
parent 3c82fbb73a
commit 80c66f8ebc
2 changed files with 18 additions and 6 deletions

View File

@ -865,16 +865,20 @@ void CheckClass::thisSubtractionError(const Token *tok)
void CheckClass::thisSubtraction()
{
const Token *tok = Token::findmatch(_tokenizer->tokens(), "this - %var%");
if (tok)
const Token *tok = _tokenizer->tokens();
for (;;)
{
tok = Token::findmatch(tok, "this - %var%");
if (!tok)
break;
if (!Token::simpleMatch(tok->previous(), "*"))
thisSubtractionError(tok);
tok = tok->next();
}
}
void CheckClass::noConstructorError(const Token *tok, const std::string &classname)
{
reportError(tok, Severity::style, "noConstructor", "The class '" + classname + "' has no constructor. Member variables not initialized.");

View File

@ -758,6 +758,14 @@ private:
{
checkThisSubtraction("; this-x ;");
ASSERT_EQUALS("[test.cpp:1]: (possible style) Suspicious pointer subtraction\n", errout.str());
checkThisSubtraction("; *this = *this-x ;");
ASSERT_EQUALS("", errout.str());
checkThisSubtraction("; *this = *this-x ;\n"
"this-x ;");
ASSERT_EQUALS("[test.cpp:2]: (possible style) Suspicious pointer subtraction\n", errout.str());
}
};