Fix #895 (false positive: Suspicious pointer subtraction)
http://sourceforge.net/apps/trac/cppcheck/ticket/895
This commit is contained in:
parent
3c82fbb73a
commit
80c66f8ebc
|
@ -865,16 +865,20 @@ void CheckClass::thisSubtractionError(const Token *tok)
|
||||||
|
|
||||||
void CheckClass::thisSubtraction()
|
void CheckClass::thisSubtraction()
|
||||||
{
|
{
|
||||||
const Token *tok = Token::findmatch(_tokenizer->tokens(), "this - %var%");
|
const Token *tok = _tokenizer->tokens();
|
||||||
if (tok)
|
for (;;)
|
||||||
{
|
{
|
||||||
|
tok = Token::findmatch(tok, "this - %var%");
|
||||||
|
if (!tok)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (!Token::simpleMatch(tok->previous(), "*"))
|
||||||
thisSubtractionError(tok);
|
thisSubtractionError(tok);
|
||||||
|
|
||||||
|
tok = tok->next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void CheckClass::noConstructorError(const Token *tok, const std::string &classname)
|
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.");
|
reportError(tok, Severity::style, "noConstructor", "The class '" + classname + "' has no constructor. Member variables not initialized.");
|
||||||
|
|
|
@ -758,6 +758,14 @@ private:
|
||||||
{
|
{
|
||||||
checkThisSubtraction("; this-x ;");
|
checkThisSubtraction("; this-x ;");
|
||||||
ASSERT_EQUALS("[test.cpp:1]: (possible style) Suspicious pointer subtraction\n", errout.str());
|
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());
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue