From 20e93f39c1bfd578f7069ae19a8844a852b63115 Mon Sep 17 00:00:00 2001 From: Samuel Benzaquen Date: Tue, 29 Apr 2014 13:41:23 +0000 Subject: [PATCH] Do not touch get() calls on 'this' object. Summary: 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(); } Reviewers: djasper Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D3540 llvm-svn: 207525 --- .../clang-tidy/misc/RedundantSmartptrGet.cpp | 1 + .../test/clang-tidy/redundant-smartptr-get.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/clang-tools-extra/clang-tidy/misc/RedundantSmartptrGet.cpp b/clang-tools-extra/clang-tidy/misc/RedundantSmartptrGet.cpp index 97398af29775..d48d74c1125e 100644 --- a/clang-tools-extra/clang-tidy/misc/RedundantSmartptrGet.cpp +++ b/clang-tools-extra/clang-tidy/misc/RedundantSmartptrGet.cpp @@ -22,6 +22,7 @@ internal::Matcher callToGet(internal::Matcher OnClass) { 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"); } diff --git a/clang-tools-extra/test/clang-tidy/redundant-smartptr-get.cpp b/clang-tools-extra/test/clang-tidy/redundant-smartptr-get.cpp index 3887f8e522dd..091e9d4dc684 100644 --- a/clang-tools-extra/test/clang-tidy/redundant-smartptr-get.cpp +++ b/clang-tools-extra/test/clang-tidy/redundant-smartptr-get.cpp @@ -81,6 +81,16 @@ void Positive() { // CHECK-NOT: warning void Negative() { + struct NegPtr { + int* get(); + int* operator->() { + return &*this->get(); + } + int& operator*() { + return *get(); + } + }; + std::unique_ptr* u; u->get()->Do();