Index: clang-tidy/bugprone/BugproneTidyModule.cpp =================================================================== --- clang-tidy/bugprone/BugproneTidyModule.cpp +++ clang-tidy/bugprone/BugproneTidyModule.cpp @@ -16,6 +16,7 @@ #include "BoolPointerImplicitConversionCheck.h" #include "CopyConstructorInitCheck.h" #include "DanglingHandleCheck.h" +#include "DeleteThisCheck.h" #include "ExceptionEscapeCheck.h" #include "FoldInitTypeCheck.h" #include "ForwardDeclarationNamespaceCheck.h" @@ -68,6 +69,8 @@ "bugprone-copy-constructor-init"); CheckFactories.registerCheck( "bugprone-dangling-handle"); + CheckFactories.registerCheck( + "bugprone-delete-this"); CheckFactories.registerCheck( "bugprone-exception-escape"); CheckFactories.registerCheck( Index: clang-tidy/bugprone/CMakeLists.txt =================================================================== --- clang-tidy/bugprone/CMakeLists.txt +++ clang-tidy/bugprone/CMakeLists.txt @@ -7,6 +7,7 @@ BugproneTidyModule.cpp CopyConstructorInitCheck.cpp DanglingHandleCheck.cpp + DeleteThisCheck.cpp ExceptionEscapeCheck.cpp FoldInitTypeCheck.cpp ForwardDeclarationNamespaceCheck.cpp Index: clang-tidy/bugprone/DeleteThisCheck.h =================================================================== --- /dev/null +++ clang-tidy/bugprone/DeleteThisCheck.h @@ -0,0 +1,35 @@ +//===--- DeleteThisCheck.h - clang-tidy--------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DELETE_THIS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DELETE_THIS_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace bugprone { + +/// Detects the usages of `delete this`. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-delete-this.html +class DeleteThisCheck : public ClangTidyCheck { +public: + DeleteThisCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace bugprone +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DELETE_THIS_H Index: clang-tidy/bugprone/DeleteThisCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/bugprone/DeleteThisCheck.cpp @@ -0,0 +1,35 @@ +//===--- DeleteThisCheck.cpp - clang-tidy----------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "DeleteThisCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace bugprone { + +void DeleteThisCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + cxxDeleteExpr( + has(ignoringParenImpCasts(cxxThisExpr()))) + .bind("DeleteThis"), + this); +} + +void DeleteThisCheck::check(const MatchFinder::MatchResult &Result) { + const auto *E = Result.Nodes.getNodeAs("DeleteThis"); + diag(E->getBeginLoc(), "usage of 'delete this' is suspicious"); +} + +} // namespace bugprone +} // namespace tidy +} // namespace clang Index: docs/clang-tidy/checks/bugprone-delete-this.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/bugprone-delete-this.rst @@ -0,0 +1,21 @@ +.. title:: clang-tidy - bugprone-delete-this + +bugprone-delete-this +==================== + +Detects the usages of ``delete this``. + +Said statement generates multiple problems, such as enforcing allocating the object via ``new``, or not allowing any +usage of instance members (neither fields nor methods) after the execution of it, nor touching ``this`` pointer +in any way. + +Example: + +.. code-block:: c++ + + class A { + void foo() { + delete this; + // warning: usage of 'delete this' is suspicious [bugprone-delete-this] + } + }