[PATCH] D21298: [Clang-tidy] delete null check

2016-11-15 Thread Gergely Angeli via cfe-commits
SilverGeri updated this revision to Diff 77972.
SilverGeri added a comment.

update tests with "CHECK-FIXES-NOT" parts


https://reviews.llvm.org/D21298

Files:
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/DeleteNullPointerCheck.cpp
  clang-tidy/readability/DeleteNullPointerCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-delete-null-pointer.rst
  test/clang-tidy/readability-delete-null-pointer.cpp

Index: test/clang-tidy/readability-delete-null-pointer.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-delete-null-pointer.cpp
@@ -0,0 +1,58 @@
+// RUN: %check_clang_tidy %s readability-delete-null-pointer %t
+
+#include 
+
+void f() {
+  int *p = 0;
+  if (p) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
+delete p;
+  }
+  // CHECK-FIXES-NOT: if (p) {
+  // CHECK-FIXES: delete p;
+
+  int *p2 = new int[3];
+  if (p2)
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
+delete[] p2;
+  // CHECK-FIXES-NOT: if (p2)
+  // CHECK-FIXES: delete[] p2;
+
+  int *p3 = 0;
+  if (NULL != p3) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
+delete p3;
+  }
+  // CHECK-FIXES-NOT: if (NULL != p3) {
+  // CHECK-FIXES: delete p3;
+
+  int *p4 = nullptr;
+  if (p4 != nullptr) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
+delete p4;
+  }
+  // CHECK-FIXES-NOT: if (p4 != nullptr) {
+  // CHECK-FIXES: delete p4;
+
+  char *c;
+  if (c != 0) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
+delete c;
+  }
+  // CHECK-FIXES-NOT: if (c != 0) {
+  // CHECK-FIXES: delete c;
+}
+
+void g() {
+  int *p5, *p6;
+  if (p5)
+delete p6;
+
+  if (p5 && p6)
+delete p5;
+
+  if (p6) {
+int x = 5;
+delete p6;
+  }
+}
\ No newline at end of file
Index: docs/clang-tidy/checks/readability-delete-null-pointer.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/readability-delete-null-pointer.rst
@@ -0,0 +1,12 @@
+.. title:: clang-tidy - readability-delete-null-pointer
+
+readability-delete-null-pointer
+===
+
+Checks the 'if' statements where a pointer's existence is checked and then deletes the pointer.
+The check is unnecessary as deleting a nullpointer has no effect.
+
+.. code:: c++
+  int *p;
+  if (p)
+delete p;
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -123,6 +123,7 @@
readability-avoid-const-params-in-decls
readability-braces-around-statements
readability-container-size-empty
+   readability-delete-null-pointer
readability-deleted-default
readability-else-after-return
readability-function-size
Index: clang-tidy/readability/ReadabilityTidyModule.cpp
===
--- clang-tidy/readability/ReadabilityTidyModule.cpp
+++ clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -13,6 +13,7 @@
 #include "AvoidConstParamsInDecls.h"
 #include "BracesAroundStatementsCheck.h"
 #include "ContainerSizeEmptyCheck.h"
+#include "DeleteNullPointerCheck.h"
 #include "DeletedDefaultCheck.h"
 #include "ElseAfterReturnCheck.h"
 #include "FunctionSizeCheck.h"
@@ -44,6 +45,8 @@
 "readability-braces-around-statements");
 CheckFactories.registerCheck(
 "readability-container-size-empty");
+CheckFactories.registerCheck(
+"readability-delete-null-pointer");
 CheckFactories.registerCheck(
 "readability-deleted-default");
 CheckFactories.registerCheck(
Index: clang-tidy/readability/DeleteNullPointerCheck.h
===
--- /dev/null
+++ clang-tidy/readability/DeleteNullPointerCheck.h
@@ -0,0 +1,35 @@
+//===--- DeleteNullPointerCheck.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_READABILITY_DELETE_NULL_POINTER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_DELETE_NULL_POINTER_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace t

[PATCH] D21298: [Clang-tidy] delete null check

2016-11-17 Thread Gergely Angeli via cfe-commits
SilverGeri added inline comments.



Comment at: test/clang-tidy/readability-delete-null-pointer.cpp:7
+  int *p = 0;
+  if (p) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; 
deleting null pointer has no effect [readability-delete-null-pointer]

aaron.ballman wrote:
> hokein wrote:
> > Does it work the case like:
> > 
> > ```
> > int *p = nullptr;
> > if (p == nullptr) {
> >p = new int[3];
> >delete[] p;
> > }
> > ```
> > 
> > ?
> Similarly, it should not mishandle a case like:
> 
> void f(int *p) {
>   if (p) {
> delete p;
>   } else {
> // Do something else
>   }
> }
it warns only if the compund statement contains only one statement (which is 
the delete). We want to warn because it is unnecessary to check the pointer 
validity if you want to just call `delete`. In other cases, we can't be sure 
about the actual behaviour.


https://reviews.llvm.org/D21298



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D21298: [Clang-tidy] delete null check

2016-11-02 Thread Gergely Angeli via cfe-commits
SilverGeri removed rL LLVM as the repository for this revision.
SilverGeri updated this revision to Diff 76803.
Herald added a subscriber: mgorny.

https://reviews.llvm.org/D21298

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DeleteNullPointerCheck.cpp
  clang-tidy/misc/DeleteNullPointerCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-delete-null-pointer.rst
  test/clang-tidy/misc-delete-null-pointer.cpp

Index: test/clang-tidy/misc-delete-null-pointer.cpp
===
--- /dev/null
+++ test/clang-tidy/misc-delete-null-pointer.cpp
@@ -0,0 +1,29 @@
+// RUN: %check_clang_tidy %s misc-delete-null-pointer %t
+
+void f() {
+  int *p = 0;
+  if (p) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if statement is unnecessary (deleting null pointer has no effect) [misc-delete-null-pointer]
+delete p;
+  }
+  // CHECK-FIXES: delete p;
+  int *p3 = new int[3];
+  if (p3)
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if statement is unnecessary (deleting null pointer has no effect) [misc-delete-null-pointer]
+delete[] p3;
+  // CHECK-FIXES: delete[] p3;
+}
+
+void g() {
+  int *p, *p2;
+  if (p)
+delete p2;
+
+  if (p && p2)
+delete p;
+
+  if (p2) {
+int x = 5;
+delete p2;
+  }
+}
Index: docs/clang-tidy/checks/misc-delete-null-pointer.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/misc-delete-null-pointer.rst
@@ -0,0 +1,12 @@
+.. title:: clang-tidy - misc-delete-null-pointer
+
+misc-delete-null-pointer
+
+
+Checks the if statements where a pointer's existence is checked and then deletes the pointer.
+The check is unnecessary as deleting a nullpointer has no effect.
+
+.. code:: c++
+int *p;
+  if (p)
+delete p;
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -58,6 +58,7 @@
misc-bool-pointer-implicit-conversion
misc-dangling-handle
misc-definitions-in-headers
+   misc-delete-null-pointer
misc-fold-init-type
misc-forward-declaration-namespace
misc-inaccurate-erase
Index: clang-tidy/misc/MiscTidyModule.cpp
===
--- clang-tidy/misc/MiscTidyModule.cpp
+++ clang-tidy/misc/MiscTidyModule.cpp
@@ -12,6 +12,7 @@
 #include "../ClangTidyModuleRegistry.h"
 #include "ArgumentCommentCheck.h"
 #include "AssertSideEffectCheck.h"
+#include "DeleteNullPointerCheck.h"
 #include "MisplacedConstCheck.h"
 #include "UnconventionalAssignOperatorCheck.h"
 #include "BoolPointerImplicitConversionCheck.h"
@@ -63,6 +64,8 @@
 CheckFactories.registerCheck("misc-argument-comment");
 CheckFactories.registerCheck(
 "misc-assert-side-effect");
+CheckFactories.registerCheck(
+"misc-delete-null-pointer");
 CheckFactories.registerCheck(
 "misc-misplaced-const");
 CheckFactories.registerCheck(
Index: clang-tidy/misc/DeleteNullPointerCheck.h
===
--- /dev/null
+++ clang-tidy/misc/DeleteNullPointerCheck.h
@@ -0,0 +1,35 @@
+//===--- DeleteNullPointerCheck.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_MISC_DELETE_NULL_POINTER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DELETE_NULL_POINTER_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace misc {
+
+/// FIXME: Write a short description.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/misc-delete-null-pointer.html
+class DeleteNullPointerCheck : public ClangTidyCheck {
+public:
+  DeleteNullPointerCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace misc
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DELETE_NULL_POINTER_H
Index: clang-tidy/misc/DeleteNullPointerCheck.cpp
===
--- /dev/null
+++ clang-tidy/misc/DeleteNullPointerCheck.cpp
@@ -0,0 +1,58 @@
+//===--- DeleteNullPointerCheck.cpp - clang-tidy---===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===---

[PATCH] D21298: [Clang-tidy] delete null check

2016-11-06 Thread Gergely Angeli via cfe-commits
SilverGeri updated this revision to Diff 76893.

https://reviews.llvm.org/D21298

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DeleteNullPointerCheck.cpp
  clang-tidy/misc/DeleteNullPointerCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-delete-null-pointer.rst
  test/clang-tidy/misc-delete-null-pointer.cpp

Index: test/clang-tidy/misc-delete-null-pointer.cpp
===
--- /dev/null
+++ test/clang-tidy/misc-delete-null-pointer.cpp
@@ -0,0 +1,29 @@
+// RUN: %check_clang_tidy %s misc-delete-null-pointer %t
+
+void f() {
+  int *p = 0;
+  if (p) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if statement is unnecessary (deleting null pointer has no effect) [misc-delete-null-pointer]
+delete p;
+  }
+  // CHECK-FIXES: delete p;
+  int *p3 = new int[3];
+  if (p3)
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if statement is unnecessary (deleting null pointer has no effect) [misc-delete-null-pointer]
+delete[] p3;
+  // CHECK-FIXES: delete[] p3;
+}
+
+void g() {
+  int *p, *p2;
+  if (p)
+delete p2;
+
+  if (p && p2)
+delete p;
+
+  if (p2) {
+int x = 5;
+delete p2;
+  }
+}
Index: docs/clang-tidy/checks/misc-delete-null-pointer.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/misc-delete-null-pointer.rst
@@ -0,0 +1,12 @@
+.. title:: clang-tidy - misc-delete-null-pointer
+
+misc-delete-null-pointer
+
+
+Checks the if statements where a pointer's existence is checked and then deletes the pointer.
+The check is unnecessary as deleting a nullpointer has no effect.
+
+.. code:: c++
+  int *p;
+  if (p)
+delete p;
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -58,6 +58,7 @@
misc-bool-pointer-implicit-conversion
misc-dangling-handle
misc-definitions-in-headers
+   misc-delete-null-pointer
misc-fold-init-type
misc-forward-declaration-namespace
misc-inaccurate-erase
Index: clang-tidy/misc/MiscTidyModule.cpp
===
--- clang-tidy/misc/MiscTidyModule.cpp
+++ clang-tidy/misc/MiscTidyModule.cpp
@@ -12,6 +12,7 @@
 #include "../ClangTidyModuleRegistry.h"
 #include "ArgumentCommentCheck.h"
 #include "AssertSideEffectCheck.h"
+#include "DeleteNullPointerCheck.h"
 #include "MisplacedConstCheck.h"
 #include "UnconventionalAssignOperatorCheck.h"
 #include "BoolPointerImplicitConversionCheck.h"
@@ -63,6 +64,8 @@
 CheckFactories.registerCheck("misc-argument-comment");
 CheckFactories.registerCheck(
 "misc-assert-side-effect");
+CheckFactories.registerCheck(
+"misc-delete-null-pointer");
 CheckFactories.registerCheck(
 "misc-misplaced-const");
 CheckFactories.registerCheck(
Index: clang-tidy/misc/DeleteNullPointerCheck.h
===
--- /dev/null
+++ clang-tidy/misc/DeleteNullPointerCheck.h
@@ -0,0 +1,35 @@
+//===--- DeleteNullPointerCheck.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_MISC_DELETE_NULL_POINTER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DELETE_NULL_POINTER_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace misc {
+
+/// FIXME: Write a short description.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/misc-delete-null-pointer.html
+class DeleteNullPointerCheck : public ClangTidyCheck {
+public:
+  DeleteNullPointerCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace misc
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DELETE_NULL_POINTER_H
Index: clang-tidy/misc/DeleteNullPointerCheck.cpp
===
--- /dev/null
+++ clang-tidy/misc/DeleteNullPointerCheck.cpp
@@ -0,0 +1,58 @@
+//===--- DeleteNullPointerCheck.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 "DeleteNullPointerCheck.

[PATCH] D21298: [Clang-tidy] delete null check

2016-11-07 Thread Gergely Angeli via cfe-commits
SilverGeri updated this revision to Diff 77015.
SilverGeri added a comment.

checks `if (p != 0)` conditions, too


https://reviews.llvm.org/D21298

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DeleteNullPointerCheck.cpp
  clang-tidy/misc/DeleteNullPointerCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-delete-null-pointer.rst
  test/clang-tidy/misc-delete-null-pointer.cpp

Index: test/clang-tidy/misc-delete-null-pointer.cpp
===
--- /dev/null
+++ test/clang-tidy/misc-delete-null-pointer.cpp
@@ -0,0 +1,49 @@
+// RUN: %check_clang_tidy %s misc-delete-null-pointer %t
+
+#include 
+
+void f() {
+  int *p = 0;
+  if (p) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if statement is unnecessary (deleting null pointer has no effect) [misc-delete-null-pointer]
+delete p;
+  }
+  // CHECK-FIXES: delete p;
+  int *p3 = new int[3];
+  if (p3)
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if statement is unnecessary (deleting null pointer has no effect) [misc-delete-null-pointer]
+delete[] p3;
+  // CHECK-FIXES: delete[] p3;
+
+  if (NULL != p) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if statement is unnecessary (deleting null pointer has no effect) [misc-delete-null-pointer]
+delete p;
+  }
+  // CHECK-FIXES: delete p;
+
+  if (p != nullptr) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if statement is unnecessary (deleting null pointer has no effect) [misc-delete-null-pointer]
+delete p;
+  }
+  // CHECK-FIXES: delete p;
+
+  if (p != 0) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if statement is unnecessary (deleting null pointer has no effect) [misc-delete-null-pointer]
+delete p;
+  }
+  // CHECK-FIXES: delete p;
+}
+
+void g() {
+  int *p, *p2;
+  if (p)
+delete p2;
+
+  if (p && p2)
+delete p;
+
+  if (p2) {
+int x = 5;
+delete p2;
+  }
+}
Index: docs/clang-tidy/checks/misc-delete-null-pointer.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/misc-delete-null-pointer.rst
@@ -0,0 +1,12 @@
+.. title:: clang-tidy - misc-delete-null-pointer
+
+misc-delete-null-pointer
+
+
+Checks the if statements where a pointer's existence is checked and then deletes the pointer.
+The check is unnecessary as deleting a nullpointer has no effect.
+
+.. code:: c++
+  int *p;
+  if (p)
+delete p;
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -58,6 +58,7 @@
misc-bool-pointer-implicit-conversion
misc-dangling-handle
misc-definitions-in-headers
+   misc-delete-null-pointer
misc-fold-init-type
misc-forward-declaration-namespace
misc-inaccurate-erase
Index: clang-tidy/misc/MiscTidyModule.cpp
===
--- clang-tidy/misc/MiscTidyModule.cpp
+++ clang-tidy/misc/MiscTidyModule.cpp
@@ -12,6 +12,7 @@
 #include "../ClangTidyModuleRegistry.h"
 #include "ArgumentCommentCheck.h"
 #include "AssertSideEffectCheck.h"
+#include "DeleteNullPointerCheck.h"
 #include "MisplacedConstCheck.h"
 #include "UnconventionalAssignOperatorCheck.h"
 #include "BoolPointerImplicitConversionCheck.h"
@@ -63,6 +64,8 @@
 CheckFactories.registerCheck("misc-argument-comment");
 CheckFactories.registerCheck(
 "misc-assert-side-effect");
+CheckFactories.registerCheck(
+"misc-delete-null-pointer");
 CheckFactories.registerCheck(
 "misc-misplaced-const");
 CheckFactories.registerCheck(
Index: clang-tidy/misc/DeleteNullPointerCheck.h
===
--- /dev/null
+++ clang-tidy/misc/DeleteNullPointerCheck.h
@@ -0,0 +1,35 @@
+//===--- DeleteNullPointerCheck.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_MISC_DELETE_NULL_POINTER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DELETE_NULL_POINTER_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace misc {
+
+/// FIXME: Write a short description.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/misc-delete-null-pointer.html
+class DeleteNullPointerCheck : public ClangTidyCheck {
+public:
+  DeleteNullPointerCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+

[PATCH] D21298: [Clang-tidy] delete null check

2016-11-14 Thread Gergely Angeli via cfe-commits
SilverGeri updated this revision to Diff 77784.
SilverGeri added a comment.
Herald added a subscriber: modocache.

move checker to readability module
add missing description to header file


https://reviews.llvm.org/D21298

Files:
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/DeleteNullPointerCheck.cpp
  clang-tidy/readability/DeleteNullPointerCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-delete-null-pointer.rst
  test/clang-tidy/readability-delete-null-pointer.cpp

Index: test/clang-tidy/readability-delete-null-pointer.cpp
===
--- /dev/null
+++ test/clang-tidy/readability-delete-null-pointer.cpp
@@ -0,0 +1,49 @@
+// RUN: %check_clang_tidy %s readability-delete-null-pointer %t
+
+#include 
+
+void f() {
+  int *p = 0;
+  if (p) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
+delete p;
+  }
+  // CHECK-FIXES: delete p;
+  int *p3 = new int[3];
+  if (p3)
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
+delete[] p3;
+  // CHECK-FIXES: delete[] p3;
+
+  if (NULL != p) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
+delete p;
+  }
+  // CHECK-FIXES: delete p;
+
+  if (p != nullptr) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
+delete p;
+  }
+  // CHECK-FIXES: delete p;
+
+  if (p != 0) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
+delete p;
+  }
+  // CHECK-FIXES: delete p;
+}
+
+void g() {
+  int *p, *p2;
+  if (p)
+delete p2;
+
+  if (p && p2)
+delete p;
+
+  if (p2) {
+int x = 5;
+delete p2;
+  }
+}
\ No newline at end of file
Index: docs/clang-tidy/checks/readability-delete-null-pointer.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/readability-delete-null-pointer.rst
@@ -0,0 +1,12 @@
+.. title:: clang-tidy - readability-delete-null-pointer
+
+readability-delete-null-pointer
+===
+
+Checks the 'if' statements where a pointer's existence is checked and then deletes the pointer.
+The check is unnecessary as deleting a nullpointer has no effect.
+
+.. code:: c++
+  int *p;
+  if (p)
+delete p;
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -123,6 +123,7 @@
readability-avoid-const-params-in-decls
readability-braces-around-statements
readability-container-size-empty
+   readability-delete-null-pointer
readability-deleted-default
readability-else-after-return
readability-function-size
Index: clang-tidy/readability/ReadabilityTidyModule.cpp
===
--- clang-tidy/readability/ReadabilityTidyModule.cpp
+++ clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -13,6 +13,7 @@
 #include "AvoidConstParamsInDecls.h"
 #include "BracesAroundStatementsCheck.h"
 #include "ContainerSizeEmptyCheck.h"
+#include "DeleteNullPointerCheck.h"
 #include "DeletedDefaultCheck.h"
 #include "ElseAfterReturnCheck.h"
 #include "FunctionSizeCheck.h"
@@ -44,6 +45,8 @@
 "readability-braces-around-statements");
 CheckFactories.registerCheck(
 "readability-container-size-empty");
+CheckFactories.registerCheck(
+"readability-delete-null-pointer");
 CheckFactories.registerCheck(
 "readability-deleted-default");
 CheckFactories.registerCheck(
Index: clang-tidy/readability/DeleteNullPointerCheck.h
===
--- /dev/null
+++ clang-tidy/readability/DeleteNullPointerCheck.h
@@ -0,0 +1,35 @@
+//===--- DeleteNullPointerCheck.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_READABILITY_DELETE_NULL_POINTER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_DELETE_NULL_POINTER_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace readability {
+
+/// Check whether the 'if' statement is unnecessary before calling 'delete' on a pointer.
+///
+/// For the user-facing documentation see:
+///

[PATCH] D21298: delete null check

2016-06-13 Thread Gergely Angeli via cfe-commits
SilverGeri created this revision.
SilverGeri added a reviewer: xazax.hun.
SilverGeri added a subscriber: cfe-commits.

removes the unnecessary if statements, if it was used to check a pointer's 
validity just to call delete on that pointer

http://reviews.llvm.org/D21298

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DeleteNullCheck.cpp
  clang-tidy/misc/DeleteNullCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-delete-null.rst
  test/clang-tidy/misc-delete-null.cpp

Index: test/clang-tidy/misc-delete-null.cpp
===
--- /dev/null
+++ test/clang-tidy/misc-delete-null.cpp
@@ -0,0 +1,30 @@
+// RUN: %check_clang_tidy %s misc-delete-null %t
+
+void f() {
+  int *p = 0;
+  if (p) {
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if statement is unnecessary (deleting null pointer has no effect) [misc-delete_null]
+delete p;
+  }
+  // CHECK-FIXES: delete p;
+
+  int *p3 = new int[3];
+  if (p3)
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: if statement is unnecessary (deleting null pointer has no effect) [misc-delete_null]
+delete[] p3;
+  // CHECK-FIXES: delete[] p3;
+}
+
+void g() {
+  int *p, *p2;
+  if (p)
+delete p2;
+
+  if (p && p2)
+delete p;
+
+  if (p2) {
+int x = 5;
+delete p2;
+  }
+}
Index: docs/clang-tidy/checks/misc-delete-null.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/misc-delete-null.rst
@@ -0,0 +1,12 @@
+.. title:: clang-tidy - misc-delete-null
+
+misc-delete-null
+
+
+Checks the if statements where a pointer's existence is checked and then deletes the pointer.
+The check is unnecessary as deleting a nullpointer has no effect.
+
+.. code:: c++
+  int *p;
+  if (p)
+delete p;
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -53,6 +53,7 @@
misc-bool-pointer-implicit-conversion
misc-dangling-handle
misc-definitions-in-headers
+   misc-delete-null
misc-fold-init-type
misc-forward-declaration-namespace
misc-inaccurate-erase
Index: clang-tidy/misc/MiscTidyModule.cpp
===
--- clang-tidy/misc/MiscTidyModule.cpp
+++ clang-tidy/misc/MiscTidyModule.cpp
@@ -16,6 +16,7 @@
 #include "BoolPointerImplicitConversionCheck.h"
 #include "DanglingHandleCheck.h"
 #include "DefinitionsInHeadersCheck.h"
+#include "DeleteNullCheck.h"
 #include "FoldInitTypeCheck.h"
 #include "ForwardDeclarationNamespaceCheck.h"
 #include "InaccurateEraseCheck.h"
@@ -69,6 +70,8 @@
 "misc-dangling-handle");
 CheckFactories.registerCheck(
 "misc-definitions-in-headers");
+CheckFactories.registerCheck(
+"misc-delete-null");
 CheckFactories.registerCheck(
 "misc-fold-init-type");
 CheckFactories.registerCheck(
Index: clang-tidy/misc/DeleteNullCheck.h
===
--- /dev/null
+++ clang-tidy/misc/DeleteNullCheck.h
@@ -0,0 +1,36 @@
+//===--- DeleteNullCheck.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_MISC_DELETE_NULL_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DELETE_NULL_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace misc {
+
+/// This checker finds if statements which check a pointer's validity
+/// and wants to free the pointer's data in the body
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/misc-delete-null.html
+class DeleteNullCheck : public ClangTidyCheck {
+public:
+  DeleteNullCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace misc
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DELETE_NULL_H
Index: clang-tidy/misc/DeleteNullCheck.cpp
===
--- /dev/null
+++ clang-tidy/misc/DeleteNullCheck.cpp
@@ -0,0 +1,65 @@
+//===--- DeleteNullCheck.cpp - clang-tidy--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===-