[clang-tools-extra] r261102 - [clang-tidy] Match the type against the get() method we are calling,

Samuel Benzaquen via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 17 08:13:15 PST 2016


Author: sbenza
Date: Wed Feb 17 10:13:14 2016
New Revision: 261102

URL: http://llvm.org/viewvc/llvm-project?rev=261102&view=rev
Log:
[clang-tidy] Match the type against the get() method we are calling,
instead of a get() method we find in the class.

The duck typed smart pointer class could have overloaded get() methods
and we should only skip the one that matches.

Modified:
    clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp?rev=261102&r1=261101&r2=261102&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp Wed Feb 17 10:13:14 2016
@@ -25,7 +25,9 @@ internal::Matcher<Expr> callToGet(intern
                                pointsTo(decl(OnClass).bind("ptr_to_ptr"))))))
                     .bind("smart_pointer")),
              unless(callee(memberExpr(hasObjectExpression(cxxThisExpr())))),
-             callee(cxxMethodDecl(hasName("get"))))
+             callee(cxxMethodDecl(
+                 hasName("get"),
+                 returns(qualType(pointsTo(type().bind("getType")))))))
       .bind("redundant_get");
 }
 
@@ -35,10 +37,8 @@ void registerMatchersForGetArrowStart(Ma
       recordDecl().bind("duck_typing"),
       has(cxxMethodDecl(hasName("operator->"),
                         returns(qualType(pointsTo(type().bind("op->Type")))))),
-      has(cxxMethodDecl(hasName("operator*"),
-                        returns(qualType(references(type().bind("op*Type")))))),
-      has(cxxMethodDecl(hasName("get"),
-                        returns(qualType(pointsTo(type().bind("getType")))))));
+      has(cxxMethodDecl(hasName("operator*"), returns(qualType(references(
+                                                  type().bind("op*Type")))))));
 
   // Catch 'ptr.get()->Foo()'
   Finder->addMatcher(memberExpr(expr().bind("memberExpr"), isArrow(),

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp?rev=261102&r1=261101&r2=261102&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp Wed Feb 17 10:13:14 2016
@@ -44,6 +44,14 @@ struct Fail2 {
   int& operator*();
 };
 
+struct PointerWithOverloadedGet {
+  int* get();
+  template <typename T>
+  T* get();
+  int* operator->();
+  int& operator*();
+};
+
 void Positive() {
   BarPtr u;
   // CHECK-FIXES: BarPtr u;
@@ -100,6 +108,11 @@ void Positive() {
   // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: redundant get() call
   // CHECK-MESSAGES: nullptr != ss->get();
   // CHECK-FIXES: bb = nullptr != *ss;
+
+  i = *PointerWithOverloadedGet().get();
+  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
+  // CHECK-MESSAGES: i = *PointerWithOverloadedGet().get();
+  // CHECK-FIXES: i = *PointerWithOverloadedGet();
 }
 
 void Negative() {
@@ -113,6 +126,8 @@ void Negative() {
     }
   };
 
+  long l = *PointerWithOverloadedGet().get<long>();
+
   std::unique_ptr<Bar>* u;
   u->get()->Do();
 




More information about the cfe-commits mailing list