1// RUN: %check_clang_tidy %s readability-ambiguous-smartptr-reset-call %t -- \
2// RUN: -config='{CheckOptions: \
3// RUN: {readability-ambiguous-smartptr-reset-call.SmartPointers: "::std::unique_ptr;::other_ptr"}}' \
4// RUN: --fix-notes -- -I %S/../modernize/Inputs/smart-ptr
5
6#include "unique_ptr.h"
7#include "shared_ptr.h"
8
9template <typename T>
10struct other_ptr {
11 T& operator*() const;
12 T* operator->() const;
13 void reset();
14};
15
16struct Resettable {
17 void reset();
18 void doSomething();
19};
20
21void Positive() {
22 std::unique_ptr<Resettable> u;
23 u.reset();
24 // 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 approach
25 // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here
26 // CHECK-FIXES: u = nullptr;
27 u->reset();
28 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach
29 // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here
30 // CHECK-FIXES: (*u).reset();
31
32 other_ptr<Resettable> s;
33 s.reset();
34 // 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 approach
35 // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider assigning the pointer to 'nullptr' here
36 // CHECK-FIXES: s = nullptr;
37 s->reset();
38 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: ambiguous call to 'reset()' on a pointee of a smart pointer, prefer more explicit approach
39 // CHECK-MESSAGES: :[[@LINE-2]]:3: note: consider dereferencing smart pointer to call 'reset' method of the pointee here
40 // CHECK-FIXES: (*s).reset();
41}
42
43void Negative() {
44 std::shared_ptr<Resettable> s_ptr;
45 s_ptr.reset();
46 s_ptr->reset();
47 s_ptr->doSomething();
48
49 std::unique_ptr<Resettable> u_ptr;
50 u_ptr.reset(nullptr);
51 u_ptr->doSomething();
52}
53

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of clang-tools-extra/test/clang-tidy/checkers/readability/ambiguous-smartptr-reset-call-custom-pointers.cpp