brintos

brintos / llvm-project-archived public Read only

0
0
Text · 19.0 KiB · e6e7eb9 Raw
398 lines · cpp
1// RUN: %check_clang_tidy %s readability-ambiguous-smartptr-reset-call %t --fix-notes -- -I %S/../modernize/Inputs/smart-ptr2 3#include "unique_ptr.h"4#include "shared_ptr.h"5 6template <typename T>7struct non_default_reset_ptr {8  T& operator*() const;9  T* operator->() const;10  void reset(T* p);11};12 13struct Resettable {14  void reset();15  void doSomething();16};17 18struct ResettableWithParam {19  void reset(int a);20  void doSomething();21};22 23struct ResettableWithDefaultParams {24  void reset(int a = 0, double b = 0.0);25  void doSomething();26};27 28struct NonResettable {29  void doSomething();30};31 32void Positive() {33  std::unique_ptr<Resettable> u;34  u.reset();35  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach36  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here37  // CHECK-FIXES: u = nullptr;38  u->reset();39  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach40  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here41  // CHECK-FIXES: (*u).reset();42 43  std::shared_ptr<Resettable> s;44  s.reset();45  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach46  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here47  // CHECK-FIXES: s = nullptr;48  s->reset();49  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach50  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here51  // CHECK-FIXES: (*s).reset();52 53  std::unique_ptr<std::unique_ptr<int>> uu_ptr;54  uu_ptr.reset();55  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach56  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here57  // CHECK-FIXES: uu_ptr = nullptr;58  uu_ptr->reset();59  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach60  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here61  // CHECK-FIXES: (*uu_ptr).reset();62 63  std::unique_ptr<std::shared_ptr<int>> su_ptr;64  su_ptr.reset();65  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach66  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here67  // CHECK-FIXES: su_ptr = nullptr;68  su_ptr->reset();69  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach70  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here71  // CHECK-FIXES: (*su_ptr).reset();72 73  std::unique_ptr<ResettableWithDefaultParams> rd;74  rd.reset();75  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach76  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here77  // CHECK-FIXES: rd = nullptr;78  rd->reset();79  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach80  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here81  // CHECK-FIXES: (*rd).reset();82 83  std::unique_ptr<std::shared_ptr<std::unique_ptr<Resettable>>> nested_ptr;84  nested_ptr.reset();85  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach86  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here87  // CHECK-FIXES: nested_ptr = nullptr;88  nested_ptr->reset();89  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach90  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here91  // CHECK-FIXES: (*nested_ptr).reset();92  (*nested_ptr).reset();93  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach94  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here95  // CHECK-FIXES: (*nested_ptr) = nullptr;96  (*nested_ptr)->reset();97  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach98  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here99  // CHECK-FIXES: (*(*nested_ptr)).reset();100  (**nested_ptr).reset();101  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach102  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here103  // CHECK-FIXES: (**nested_ptr) = nullptr;104  (**nested_ptr)->reset();105  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach106  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here107  // CHECK-FIXES: (*(**nested_ptr)).reset();108}109 110void Negative() {111  std::unique_ptr<Resettable> u_ptr;112  u_ptr.reset(nullptr);113  u_ptr->doSomething();114 115  std::shared_ptr<Resettable> s_ptr;116  s_ptr.reset(nullptr);117  s_ptr->doSomething();118  119  Resettable* raw_ptr;120  raw_ptr->reset();121  raw_ptr->doSomething();122 123  Resettable resettable;124  resettable.reset();125  resettable.doSomething();126  127  std::unique_ptr<ResettableWithParam> u_ptr_param;128  u_ptr_param.reset();129  u_ptr_param.reset(nullptr);130  u_ptr_param->reset(0);131 132  std::unique_ptr<NonResettable> u_ptr_no_reset;133  u_ptr_no_reset.reset();134 135  std::shared_ptr<ResettableWithParam> s_ptr_param;136  s_ptr_param.reset();137  s_ptr_param->reset(0);138  s_ptr_param->doSomething();139 140  std::shared_ptr<NonResettable> s_ptr_no_reset;141  s_ptr_no_reset.reset();142 143  std::unique_ptr<ResettableWithDefaultParams> u_ptr_default_params;144  u_ptr_default_params.reset(nullptr);145  u_ptr_default_params->reset(1);146  u_ptr_default_params->reset(1, 2.0);147 148  non_default_reset_ptr<Resettable> non_default_reset_ptr;149  non_default_reset_ptr.reset(new Resettable);150  non_default_reset_ptr->reset();151}152 153template <typename T>154void TemplatePositiveTest() {155  std::unique_ptr<T> u_ptr;156 157  u_ptr.reset();158  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach159  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here160  // CHECK-FIXES: u_ptr = nullptr;161  u_ptr->reset();162  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach163  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here164  // CHECK-FIXES: (*u_ptr).reset();165 166  std::shared_ptr<T> s_ptr;167  s_ptr.reset();168  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach169  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here170  // CHECK-FIXES: s_ptr = nullptr;171  s_ptr->reset();172  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach173  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here174  // CHECK-FIXES: (*s_ptr).reset();175}176 177template <typename T>178void TemplatNegativeTestTypeWithReset() {179  std::unique_ptr<T> u_ptr;180  u_ptr.reset();181  u_ptr->reset(0);182 183  std::shared_ptr<T> s_ptr;184  s_ptr.reset();185  s_ptr->reset(0);186}187 188template <typename T>189void TemplatNegativeTestTypeWithoutReset() {190  std::unique_ptr<T> u_ptr;191  u_ptr.reset();192 193  std::unique_ptr<T> s_ptr;194  s_ptr.reset();195}196 197void instantiate() {198  TemplatePositiveTest<Resettable>();199  TemplatePositiveTest<std::unique_ptr<int>>();200  TemplatePositiveTest<std::shared_ptr<int>>();201  TemplatNegativeTestTypeWithReset<ResettableWithParam>();202  TemplatNegativeTestTypeWithoutReset<NonResettable>();203}204 205struct S {206  void foo() {207    u_ptr.reset();208    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach209    // CHECK-MESSAGES: :[[@LINE-2]]:5: note: consider assigning the pointer to 'nullptr' here210    // CHECK-FIXES: u_ptr = nullptr;211    u_ptr->reset();212    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach213    // CHECK-MESSAGES: :[[@LINE-2]]:5: note: consider dereferencing smart pointer to call 'reset' method of the pointee here214    // CHECK-FIXES: (*u_ptr).reset();215    u_ptr.reset(nullptr);216    u_ptr->doSomething();217 218    s_ptr.reset();219    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach220    // CHECK-MESSAGES: :[[@LINE-2]]:5: note: consider assigning the pointer to 'nullptr' here221    // CHECK-FIXES: s_ptr = nullptr;222    s_ptr->reset();223    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach224    // CHECK-MESSAGES: :[[@LINE-2]]:5: note: consider dereferencing smart pointer to call 'reset' method of the pointee here225    // CHECK-FIXES: (*s_ptr).reset();226    s_ptr.reset(nullptr);227 228    ptr.reset();229  }230 231  std::unique_ptr<Resettable> u_ptr;232  std::unique_ptr<std::shared_ptr<int>> s_ptr;233  std::unique_ptr<NonResettable> ptr;234};235 236 237typedef std::unique_ptr<Resettable> TypedefResettableUniquePtr;238typedef std::shared_ptr<Resettable> TypedefResettableSharedPtr;239 240void TypedefPositive() {241  TypedefResettableUniquePtr u_ptr;242  u_ptr.reset();243  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach244  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here245  // CHECK-FIXES: u_ptr = nullptr;246  u_ptr->reset();247  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach248  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here249  // CHECK-FIXES: (*u_ptr).reset();250 251  TypedefResettableSharedPtr s_ptr;252  s_ptr.reset();253  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach254  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here255  // CHECK-FIXES: s_ptr = nullptr;256 257  s_ptr->reset();258  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach259  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here260  // CHECK-FIXES: (*s_ptr).reset();261}262 263using UsingResettableUniquePtr = std::unique_ptr<Resettable>;264using UsingResettableSharedPtr = std::shared_ptr<Resettable>;265 266void UsingPositive() {267  UsingResettableUniquePtr u_ptr;268  u_ptr.reset();269  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach270  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here271  // CHECK-FIXES: u_ptr = nullptr;272  u_ptr->reset();273  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach274  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here275  // CHECK-FIXES: (*u_ptr).reset();276 277  UsingResettableSharedPtr s_ptr;278  s_ptr.reset();279  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach280  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here281  // CHECK-FIXES: s_ptr = nullptr;282 283  s_ptr->reset();284  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach285  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here286  // CHECK-FIXES: (*s_ptr).reset();287}288 289template<typename T>290using UsingUniquePtr = std::unique_ptr<T>;291template<typename T>292using UsingSharedPtr = std::shared_ptr<T>;293 294void UsingTemplatePositive() {295  UsingUniquePtr<Resettable> u_ptr;296  u_ptr.reset();297  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach298  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here299  // CHECK-FIXES: u_ptr = nullptr;300  u_ptr->reset();301  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach302  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here303  // CHECK-FIXES: (*u_ptr).reset();304 305  UsingSharedPtr<Resettable> s_ptr;306  s_ptr.reset();307  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach308  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here309  // CHECK-FIXES: s_ptr = nullptr;310 311  s_ptr->reset();312  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach313  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here314  // CHECK-FIXES: (*s_ptr).reset();315}316 317template<typename T>318void UsingByTemplatePositive() {319  UsingUniquePtr<T> u_ptr;320  u_ptr.reset();321  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach322  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here323  // CHECK-FIXES: u_ptr = nullptr;324  u_ptr->reset();325  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach326  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here327  // CHECK-FIXES: (*u_ptr).reset();328 329  UsingSharedPtr<T> s_ptr;330  s_ptr.reset();331  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach332  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here333  // CHECK-FIXES: s_ptr = nullptr;334 335  s_ptr->reset();336  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach337  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here338  // CHECK-FIXES: (*s_ptr).reset();339}340 341void instantiate2() {342  UsingByTemplatePositive<Resettable>();343}344 345void NestedUsingPositive() {346  UsingUniquePtr<UsingSharedPtr<TypedefResettableUniquePtr>> nested_ptr;347  nested_ptr.reset();348  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach349  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here350  // CHECK-FIXES: nested_ptr = nullptr;351  nested_ptr->reset();352  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach353  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here354  // CHECK-FIXES: (*nested_ptr).reset();355  (*nested_ptr).reset();356  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach357  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here358  // CHECK-FIXES: (*nested_ptr) = nullptr;359  (*nested_ptr)->reset();360  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach361  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here362  // CHECK-FIXES: (*(*nested_ptr)).reset();363  (**nested_ptr).reset();364  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach365  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here366  // CHECK-FIXES: (**nested_ptr) = nullptr;367  (**nested_ptr)->reset();368  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach369  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here370  // CHECK-FIXES: (*(**nested_ptr)).reset();371}372 373// Check other default pointers and classes.374namespace boost {375 376template <typename T>377struct shared_ptr {378  T& operator*() const;379  T* operator->() const;380  void reset();381  void reset(T*);382};383 384} // namespace boost385 386void PositiveOtherClasses() {387  boost::shared_ptr<Resettable> sh;388  sh.reset();389  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a smart pointer with pointee that also has a 'reset()' method, prefer more explicit approach390  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here391  // CHECK-FIXES: sh = nullptr;392  sh->reset();393  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach394  // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here395  // CHECK-FIXES: (*sh).reset();396}397 398