[PATCH] Do not touch get() calls on 'this' object.

Samuel Benzaquen sbenza at google.com
Tue Apr 29 06:32:57 PDT 2014


Hi djasper,

These calls are part of the implementation of the smart pointer itself
and chaning it is likely to be wrong.
Example:
  T& operator*() const { return *get(); }

http://reviews.llvm.org/D3540

Files:
  clang-tidy/misc/RedundantSmartptrGet.cpp
  test/clang-tidy/redundant-smartptr-get.cpp

Index: clang-tidy/misc/RedundantSmartptrGet.cpp
===================================================================
--- clang-tidy/misc/RedundantSmartptrGet.cpp
+++ clang-tidy/misc/RedundantSmartptrGet.cpp
@@ -22,6 +22,7 @@
              on(expr(anyOf(hasType(OnClass),
                            hasType(qualType(pointsTo(decl(OnClass).bind(
                                "ptr_to_ptr")))))).bind("smart_pointer")),
+             unless(callee(memberExpr(hasObjectExpression(thisExpr())))),
              callee(methodDecl(hasName("get")))).bind("redundant_get");
 }
 
Index: test/clang-tidy/redundant-smartptr-get.cpp
===================================================================
--- test/clang-tidy/redundant-smartptr-get.cpp
+++ test/clang-tidy/redundant-smartptr-get.cpp
@@ -81,6 +81,16 @@
 // CHECK-NOT: warning
 
 void Negative() {
+  struct NegPtr {
+    int* get();
+    int* operator->() {
+      return &*this->get();
+    }
+    int& operator*() {
+      return *get();
+    }
+  };
+
   std::unique_ptr<Bar>* u;
   u->get()->Do();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3540.8917.patch
Type: text/x-patch
Size: 1054 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140429/24904bec/attachment.bin>


More information about the cfe-commits mailing list