1 // RUN: %check_clang_tidy %s readability-redundant-smartptr-get %t
2
3 #define NULL __null
4
5 namespace std {
6
7 // MSVC headers define operator templates instead of plain operators.
8
9 template <typename T>
10 struct unique_ptr {
11 template <typename T2 = T>
12 T2& operator*() const;
13 template <typename T2 = T>
14 T2* operator->() const;
15 T* get() const;
16 explicit operator bool() const noexcept;
17 };
18
19 template <typename T>
20 struct shared_ptr {
21 template <typename T2 = T>
22 T2& operator*() const;
23 template <typename T2 = T>
24 T2* operator->() const;
25 T* get() const;
26 explicit operator bool() const noexcept;
27 };
28
29 } // namespace std
30
31 struct Bar {
32 void Do();
33 void ConstDo() const;
34 };
35
Positive()36 void Positive() {
37 std::unique_ptr<Bar>* up;
38 (*up->get()).Do();
39 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant get() call
40 // CHECK-MESSAGES: (*up->get()).Do();
41 // CHECK-FIXES: (**up).Do();
42
43 std::unique_ptr<int> uu;
44 std::shared_ptr<double> *ss;
45 bool bb = uu.get() == nullptr;
46 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant get() call
47 // CHECK-MESSAGES: uu.get() == nullptr;
48 // CHECK-FIXES: bool bb = uu == nullptr;
49
50 if (up->get());
51 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
52 // CHECK-MESSAGES: if (up->get());
53 // CHECK-FIXES: if (*up);
54 if ((uu.get()));
55 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
56 // CHECK-MESSAGES: if ((uu.get()));
57 // CHECK-FIXES: if ((uu));
58 bb = !ss->get();
59 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant get() call
60 // CHECK-MESSAGES: bb = !ss->get();
61 // CHECK-FIXES: bb = !*ss;
62
63 bb = nullptr != ss->get();
64 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: redundant get() call
65 // CHECK-MESSAGES: nullptr != ss->get();
66 // CHECK-FIXES: bb = nullptr != *ss;
67
68 bb = std::unique_ptr<int>().get() == NULL;
69 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
70 // CHECK-MESSAGES: bb = std::unique_ptr<int>().get() == NULL;
71 // CHECK-FIXES: bb = std::unique_ptr<int>() == NULL;
72 bb = ss->get() == NULL;
73 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
74 // CHECK-MESSAGES: bb = ss->get() == NULL;
75 // CHECK-FIXES: bb = *ss == NULL;
76
77 std::unique_ptr<int> x, y;
78 if (x.get() == nullptr);
79 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
80 // CHECK-MESSAGES: if (x.get() == nullptr);
81 // CHECK-FIXES: if (x == nullptr);
82 if (nullptr == y.get());
83 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: redundant get() call
84 // CHECK-MESSAGES: if (nullptr == y.get());
85 // CHECK-FIXES: if (nullptr == y);
86 if (x.get() == NULL);
87 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
88 // CHECK-MESSAGES: if (x.get() == NULL);
89 // CHECK-FIXES: if (x == NULL);
90 if (NULL == x.get());
91 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: redundant get() call
92 // CHECK-MESSAGES: if (NULL == x.get());
93 // CHECK-FIXES: if (NULL == x);
94 }
95