Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions lib/checkother.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class CPPCHECKLIB CheckOther : public Check {
checkOther.checkAccessOfMovedVariable();
checkOther.checkModuloOfOne();
checkOther.checkOverlappingWrite();
checkOther.checkDeletionOfThis();
}

/** @brief Clarify calculation for ".. a * b ? .." */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -362,6 +366,8 @@ class CPPCHECKLIB CheckOther : public Check {
const std::vector<const Token *> nullvec;
c.funcArgOrderDifferent("function", nullptr, nullptr, nullvec, nullvec);
c.checkModuloOfOneError(nullptr);

c.checkDeletionOfThisError(nullptr);
}

static std::string myName() {
Expand Down