From f85878f1ea227e3098eae5eae5d7a94762937af5 Mon Sep 17 00:00:00 2001 From: Davi Feliciano Nonnenmacher Date: Thu, 11 Apr 2024 23:34:13 -0300 Subject: [PATCH] Added check for deletion of this (#1136) --- lib/checkother.cpp | 19 +++++++++++++++++++ lib/checkother.h | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 145693b504c..ea5b93eab8b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3861,6 +3861,11 @@ void CheckOther::checkModuloOfOneError(const Token *tok) reportError(tok, Severity::style, "moduloofone", "Modulo of one is always equal to zero"); } +void CheckOther::checkDeletionOfThisError(const Token* tok) +{ + reportError(tok, Severity::warning, "deletethis", "Deletion of this should never happen"); +} + //----------------------------------------------------------------------------- // Overlapping write (undefined behavior) //----------------------------------------------------------------------------- @@ -4006,3 +4011,17 @@ void CheckOther::overlappingWriteFunction(const Token *tok) const std::string &funcname = tok ? tok->str() : emptyString; reportError(tok, Severity::error, "overlappingWriteFunction", "Overlapping read/write in " + funcname + "() is undefined behavior"); } + +void CheckOther::checkDeletionOfThis() +{ + if (!mSettings->severity.isEnabled(Severity::warning) && !mSettings->isPremiumEnabled("deletethis")) + return; + + logChecker("CheckOther::checkDeleteThis"); // warning + + for (const Token* tok = mTokenizer->tokens(); tok; tok = tok->next()) { + if (!tok->next()) break; + if (tok->str() == "delete" && tok->next()->str() == "this") + checkDeletionOfThisError(tok); + } +} \ No newline at end of file diff --git a/lib/checkother.h b/lib/checkother.h index 1b7d080e7e5..f6128bde3e9 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -113,6 +113,7 @@ class CPPCHECKLIB CheckOther : public Check { checkOther.checkAccessOfMovedVariable(); checkOther.checkModuloOfOne(); checkOther.checkOverlappingWrite(); + checkOther.checkDeletionOfThis(); } /** @brief Clarify calculation for ".. a * b ? .." */ @@ -234,6 +235,8 @@ class CPPCHECKLIB CheckOther : public Check { void overlappingWriteUnion(const Token *tok); void overlappingWriteFunction(const Token *tok); + void checkDeletionOfThis(); + // Error messages.. void checkComparisonFunctionIsAlwaysTrueOrFalseError(const Token* tok, const std::string &functionName, const std::string &varName, const bool result); void checkCastIntToCharAndBackError(const Token *tok, const std::string &strFunctionName); @@ -289,6 +292,7 @@ class CPPCHECKLIB CheckOther : public Check { void knownPointerToBoolError(const Token* tok, const ValueFlow::Value* value); void comparePointersError(const Token *tok, const ValueFlow::Value *v1, const ValueFlow::Value *v2); void checkModuloOfOneError(const Token *tok); + void checkDeletionOfThisError(const Token* tok); void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override { CheckOther c(nullptr, settings, errorLogger); @@ -362,6 +366,8 @@ class CPPCHECKLIB CheckOther : public Check { const std::vector nullvec; c.funcArgOrderDifferent("function", nullptr, nullptr, nullvec, nullvec); c.checkModuloOfOneError(nullptr); + + c.checkDeletionOfThisError(nullptr); } static std::string myName() {