[PATCH] D21298: [Clang-tidy] delete null check
SilverGeri updated this revision to Diff 81452. SilverGeri added a comment. remove unused string using early exit in condition shorten check-message lines add check-fisex to 'else' part 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,73 @@ +// RUN: %check_clang_tidy %s readability-delete-null-pointer %t + +#define NULL 0 + +void f() { + int *p = 0; + // CHECK-FIXES: // comment that should not be deleted + if (p) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer] +// comment that should not be deleted +delete p; + } + // CHECK-FIXES-NOT: if (p) { + // CHECK-FIXES: delete p; + + + int *p2 = new int[3]; + // CHECK-FIXES: // another comment to keep + if (p2) +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; +// another comment to keep +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; +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; +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; +delete c; + } + // CHECK-FIXES-NOT: if (c != 0) { + // CHECK-FIXES: delete c; + + char *c2; + if (c2) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; +// CHECK-FIXES: } else { +// CHECK-FIXES: c2 = c; +delete c2; + } else { +c2 = c; + } +} + +void g() { + int *p5, *p6; + if (p5) +delete p6; + + if (p5 && p6) +delete p5; + + if (p6) { +int x = 5; +delete p6; + } +} 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 @@ -127,6 +127,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" @@ -45,6 +46,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_DEL
[PATCH] D21298: [Clang-tidy] delete null check
SilverGeri updated this revision to Diff 81458. 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,73 @@ +// RUN: %check_clang_tidy %s readability-delete-null-pointer %t + +#define NULL 0 + +void f() { + int *p = 0; + // CHECK-FIXES: // comment that should not be deleted + if (p) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer] +// comment that should not be deleted +delete p; + } + // CHECK-FIXES-NOT: if (p) { + // CHECK-FIXES: delete p; + + + int *p2 = new int[3]; + // CHECK-FIXES: // another comment to keep + if (p2) +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; +// another comment to keep +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; +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; +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; +delete c; + } + // CHECK-FIXES-NOT: if (c != 0) { + // CHECK-FIXES: delete c; + + char *c2; + if (c2) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; +// CHECK-FIXES: } else { +// CHECK-FIXES: c2 = c; +delete c2; + } else { +c2 = c; + } +} + +void g() { + int *p5, *p6; + if (p5) +delete p6; + + if (p5 && p6) +delete p5; + + if (p6) { +int x = 5; +delete p6; + } +} 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 @@ -127,6 +127,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" @@ -45,6 +46,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 cl
[PATCH] D21298: [Clang-tidy] delete null check
SilverGeri updated this revision to Diff 81721. SilverGeri added a comment. removing redundant `allOf` from `ifStmt` 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,73 @@ +// RUN: %check_clang_tidy %s readability-delete-null-pointer %t + +#define NULL 0 + +void f() { + int *p = 0; + // CHECK-FIXES: // comment that should not be deleted + if (p) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer] +// comment that should not be deleted +delete p; + } + // CHECK-FIXES-NOT: if (p) { + // CHECK-FIXES: delete p; + + + int *p2 = new int[3]; + // CHECK-FIXES: // another comment to keep + if (p2) +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; +// another comment to keep +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; +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; +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; +delete c; + } + // CHECK-FIXES-NOT: if (c != 0) { + // CHECK-FIXES: delete c; + + char *c2; + if (c2) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; +// CHECK-FIXES: } else { +// CHECK-FIXES: c2 = c; +delete c2; + } else { +c2 = c; + } +} + +void g() { + int *p5, *p6; + if (p5) +delete p6; + + if (p5 && p6) +delete p5; + + if (p6) { +int x = 5; +delete p6; + } +} 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 @@ -127,6 +127,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" @@ -45,6 +46,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_READABI
[PATCH] D21298: [Clang-tidy] delete null check
SilverGeri updated this revision to Diff 82401. SilverGeri added a comment. remove brackets 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,73 @@ +// RUN: %check_clang_tidy %s readability-delete-null-pointer %t + +#define NULL 0 + +void f() { + int *p = 0; + // CHECK-FIXES: // comment that should not be deleted + if (p) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer] +// comment that should not be deleted +delete p; + } + // CHECK-FIXES-NOT: if (p) { + // CHECK-FIXES: delete p; + + + int *p2 = new int[3]; + // CHECK-FIXES: // another comment to keep + if (p2) +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; +// another comment to keep +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; +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; +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; +delete c; + } + // CHECK-FIXES-NOT: if (c != 0) { + // CHECK-FIXES: delete c; + + char *c2; + if (c2) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; +// CHECK-FIXES: } else { +// CHECK-FIXES: c2 = c; +delete c2; + } else { +c2 = c; + } +} + +void g() { + int *p5, *p6; + if (p5) +delete p6; + + if (p5 && p6) +delete p5; + + if (p6) { +int x = 5; +delete p6; + } +} 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 @@ -127,6 +127,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" @@ -45,6 +46,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_
[PATCH] D21298: [Clang-tidy] delete null check
SilverGeri updated this revision to Diff 82732. SilverGeri added a comment. remove redundant `allOf` statements; refactor test's comment checking part 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,76 @@ +// RUN: %check_clang_tidy %s readability-delete-null-pointer %t + +#define NULL 0 + +void f() { + int *p = 0; + + // comment that should not be deleted #1 + if (p) { // comment that should not be deleted #2 +delete p; + } // comment that should not be deleted #3 + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer] + + // CHECK-FIXES: {{^ }}// comment that should not be deleted #1 + // CHECK-FIXES-NEXT: {{^ }}// comment that should not be deleted #2 + // CHECK-FIXES-NEXT: delete p; + // CHECK-FIXES-NEXT: {{^ }}// comment that should not be deleted #3 + + int *p2 = new int[3]; + // comment that should not be deleted #4 + if (p2) // comment that should not be deleted #5 +delete[] p2; + // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: 'if' statement is unnecessary; + + // CHECK-FIXES: // comment that should not be deleted #4 + // CHECK-FIXES-NEXT: {{^ }}// comment that should not be deleted #5 + // CHECK-FIXES-NEXT: delete[] p2; + + int *p3 = 0; + if (NULL != p3) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; +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; +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; +delete c; + } + // CHECK-FIXES-NOT: if (c != 0) { + // CHECK-FIXES: delete c; + + char *c2; + if (c2) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; +// CHECK-FIXES: } else { +// CHECK-FIXES: c2 = c; +delete c2; + } else { +c2 = c; + } +} + +void g() { + int *p5, *p6; + if (p5) +delete p6; + + if (p5 && p6) +delete p5; + + if (p6) { +int x = 5; +delete p6; + } +} Index: docs/clang-tidy/checks/readability-delete-null-pointer.rst === --- /dev/null +++ docs/clang-tidy/checks/readability-delete-null-pointer.rst @@ -0,0 +1,13 @@ +.. 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 ``null pointer`` 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 @@ -127,6 +127,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" @@ -45,6 +46,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++ -*-===// +// +// Th
[PATCH] D21298: [Clang-tidy] delete null check
SilverGeri updated this revision to Diff 82760. SilverGeri added a comment. reduce number `hasCondition` to 1; add FIXME comment; shorten check comments in test 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,76 @@ +// RUN: %check_clang_tidy %s readability-delete-null-pointer %t + +#define NULL 0 + +void f() { + int *p = 0; + + // #1 + if (p) { // #2 +delete p; + } // #3 + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer] + + // CHECK-FIXES: {{^ }}// #1 + // CHECK-FIXES-NEXT: {{^ }}// #2 + // CHECK-FIXES-NEXT: delete p; + // CHECK-FIXES-NEXT: {{^ }}// #3 + + int *p2 = new int[3]; + // #4 + if (p2) // #5 +delete[] p2; + // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: 'if' statement is unnecessary; + + // CHECK-FIXES: // #4 + // CHECK-FIXES-NEXT: {{^ }}// #5 + // CHECK-FIXES-NEXT: delete[] p2; + + int *p3 = 0; + if (NULL != p3) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; +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; +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; +delete c; + } + // CHECK-FIXES-NOT: if (c != 0) { + // CHECK-FIXES: delete c; + + char *c2; + if (c2) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; +// CHECK-FIXES: } else { +// CHECK-FIXES: c2 = c; +delete c2; + } else { +c2 = c; + } +} + +void g() { + int *p5, *p6; + if (p5) +delete p6; + + if (p5 && p6) +delete p5; + + if (p6) { +int x = 5; +delete p6; + } +} Index: docs/clang-tidy/checks/readability-delete-null-pointer.rst === --- /dev/null +++ docs/clang-tidy/checks/readability-delete-null-pointer.rst @@ -0,0 +1,13 @@ +.. 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 null pointer 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 @@ -127,6 +127,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" @@ -45,6 +46,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_TOOL
[PATCH] D21298: [Clang-tidy] delete null check
SilverGeri marked 5 inline comments as done. SilverGeri added inline comments. Comment at: docs/clang-tidy/checks/readability-delete-null-pointer.rst:7 +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. + alexfh wrote: > alexfh wrote: > > Either `null pointer` or `nullptr` (enclosed in double backquotes). > Sorry for not being clear enough: "null pointer" is not an inline code > snippet, it shouldn't be enclosed in double backquotes or anything else. The > "(enclosed in double backquotes)" part was meant to apply to `nullptr` only > (since it's a keyword and should be highlighted as a code snippet). To be honest, I don't even understand why I did what I did... :D 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
SilverGeri updated this revision to Diff 79338. SilverGeri added a comment. Herald added a subscriber: JDevlieghere. only warn, not fix when the 'if' statement has 'else' clause keeping comments 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,71 @@ +// RUN: %check_clang_tidy %s readability-delete-null-pointer %t + +#define NULL 0 + +void f() { + int *p = 0; + // CHECK-FIXES: // comment that should not be deleted + if (p) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer] +// comment that should not be deleted +delete p; + } + // CHECK-FIXES-NOT: if (p) { + // CHECK-FIXES: delete p; + + + int *p2 = new int[3]; + // CHECK-FIXES: // another comment to keep + if (p2) +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer] +// another comment to keep +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; + + char *c2; + if (c2) { +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer] +delete c2; + } else { +c2 = c; + } +} + +void g() { + int *p5, *p6; + if (p5) +delete p6; + + if (p5 && p6) +delete p5; + + if (p6) { +int x = 5; +delete p6; + } +} 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 @@ -127,6 +127,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" @@ -45,6 +46,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++ -*-===// +// +//