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()); + } };