From 80c66f8ebc728a23db283abdd8d29685241c54f1 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Wed, 4 Nov 2009 21:36:27 +0200 Subject: [PATCH] Fix #895 (false positive: Suspicious pointer subtraction) http://sourceforge.net/apps/trac/cppcheck/ticket/895 --- lib/checkclass.cpp | 16 ++++++++++------ test/testclass.cpp | 8 ++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 442216241..de51ed8a7 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -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 (;;) { - thisSubtractionError(tok); + 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."); diff --git a/test/testclass.cpp b/test/testclass.cpp index bc8538e87..9d989904d 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -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()); + } };