[clang-tools-extra] r294912 - [clang-tidy] Fix for bug 31838: readability-delete-null-pointer does not work for class members

Mads Ravn via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 12 12:10:00 PST 2017


Author: madsravn
Date: Sun Feb 12 14:09:59 2017
New Revision: 294912

URL: http://llvm.org/viewvc/llvm-project?rev=294912&view=rev
Log:
[clang-tidy] Fix for bug 31838: readability-delete-null-pointer does not work for class members

I have made a small fix for readability-delete-null-pointer check so it also checks for class members.

Example of case that it fixes
```
  struct A {
    void foo() {
      if(mp)
        delete mp;
    }
    int *mp;
  };
```

Reviewers: JDevlieghere, aaron.ballman, alexfh, malcolm.parsons

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D29726

Modified:
    clang-tools-extra/trunk/clang-tidy/readability/DeleteNullPointerCheck.cpp
    clang-tools-extra/trunk/clang-tidy/readability/DeleteNullPointerCheck.h
    clang-tools-extra/trunk/test/clang-tidy/readability-delete-null-pointer.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/DeleteNullPointerCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/DeleteNullPointerCheck.cpp?rev=294912&r1=294911&r2=294912&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/DeleteNullPointerCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/DeleteNullPointerCheck.cpp Sun Feb 12 14:09:59 2017
@@ -24,8 +24,15 @@ void DeleteNullPointerCheck::registerMat
                         to(decl(equalsBoundNode("deletedPointer"))))))))
           .bind("deleteExpr");
 
-  const auto PointerExpr =
-      ignoringImpCasts(declRefExpr(to(decl().bind("deletedPointer"))));
+  const auto DeleteMemberExpr =
+      cxxDeleteExpr(has(castExpr(has(memberExpr(hasDeclaration(
+                        fieldDecl(equalsBoundNode("deletedMemberPointer"))))))))
+          .bind("deleteMemberExpr");
+
+  const auto PointerExpr = ignoringImpCasts(anyOf(
+      declRefExpr(to(decl().bind("deletedPointer"))),
+      memberExpr(hasDeclaration(fieldDecl().bind("deletedMemberPointer")))));
+
   const auto PointerCondition = castExpr(hasCastKind(CK_PointerToBoolean),
                                          hasSourceExpression(PointerExpr));
   const auto BinaryPointerCheckCondition =
@@ -34,9 +41,11 @@ void DeleteNullPointerCheck::registerMat
 
   Finder->addMatcher(
       ifStmt(hasCondition(anyOf(PointerCondition, BinaryPointerCheckCondition)),
-             hasThen(anyOf(DeleteExpr,
-                           compoundStmt(has(DeleteExpr), statementCountIs(1))
-                               .bind("compound"))))
+             hasThen(anyOf(
+                 DeleteExpr, DeleteMemberExpr,
+                 compoundStmt(has(anyOf(DeleteExpr, DeleteMemberExpr)),
+                              statementCountIs(1))
+                     .bind("compound"))))
           .bind("ifWithDelete"),
       this);
 }

Modified: clang-tools-extra/trunk/clang-tidy/readability/DeleteNullPointerCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/DeleteNullPointerCheck.h?rev=294912&r1=294911&r2=294912&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/DeleteNullPointerCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/DeleteNullPointerCheck.h Sun Feb 12 14:09:59 2017
@@ -16,7 +16,8 @@ namespace clang {
 namespace tidy {
 namespace readability {
 
-/// Check whether the 'if' statement is unnecessary before calling 'delete' on a pointer.
+/// Check whether the 'if' statement is unnecessary before calling 'delete' on a
+/// pointer.
 ///
 /// For the user-facing documentation see:
 /// http://clang.llvm.org/extra/clang-tidy/checks/readability-delete-null-pointer.html

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-delete-null-pointer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-delete-null-pointer.cpp?rev=294912&r1=294911&r2=294912&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-delete-null-pointer.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-delete-null-pointer.cpp Sun Feb 12 14:09:59 2017
@@ -59,6 +59,16 @@ void f() {
   } else {
     c2 = c;
   }
+  struct A {
+    void foo() {
+      if (mp) // #6
+        delete mp;
+      // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: 'if' statement is unnecessary; deleting null pointer has no effect [readability-delete-null-pointer]
+      // CHECK-FIXES: {{^      }}// #6
+      // CHECK-FIXES-NEXT: delete mp;
+    }
+    int *mp;
+  };
 }
 
 void g() {




More information about the cfe-commits mailing list