summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/redundant-smartptr-get.cpp
blob: b43e38b3da7ec8531ab9a53a6281cf7282687a7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// RUN: clang-tidy --checks=misc-redundant-smartptr-get %s -- | FileCheck %s

// CHECK-NOT: warning

namespace std {

template <typename T>
struct MakeRef {
  typedef T& type;
};

template <typename T>
struct unique_ptr {
  T* get();
  T* operator->();
  // This simulates libstdc++'s implementation of unique_ptr.
  typename MakeRef<T>::type operator*();
};
}  // namespace std

struct int_ptr {
  int* get();
  int* operator->();
  int& operator*();
};

struct Bar {
  void Do();
  void ConstDo() const;
};

struct Fail1 {
  Bar* get();
};
struct Fail2 {
  Bar* get();
  int* operator->();
  int& operator*();
};

void Positive() {
  std::unique_ptr<Bar> u;
  std::unique_ptr<Bar>().get()->Do();
  // CHECK: :[[@LINE-1]]:3: warning: Redundant get() call on smart pointer. [misc-redundant-smartptr-get]
  // CHECK: std::unique_ptr<Bar>().get()->Do();

  u.get()->ConstDo();
  // CHECK: :[[@LINE-1]]:3: warning: Redundant get() call on smart pointer.
  // CHECK: u.get()->ConstDo();

  Bar& b = *std::unique_ptr<Bar>().get();
  // CHECK: :[[@LINE-1]]:13: warning: Redundant get() call on smart pointer.
  // CHECK: Bar& b = *std::unique_ptr<Bar>().get();

  (*std::unique_ptr<Bar>().get()).ConstDo();
  // CHECK: :[[@LINE-1]]:5: warning: Redundant get() call on smart pointer.
  // CHECK: (*std::unique_ptr<Bar>().get()).ConstDo();

  std::unique_ptr<Bar>* up;
  (*up->get()).Do();
  // CHECK: :[[@LINE-1]]:5: warning: Redundant get() call on smart pointer.
  // CHECK: (*up->get()).Do();

  int_ptr ip;
  int i = *ip.get();
  // CHECK: :[[@LINE-1]]:12: warning: Redundant get() call on smart pointer.
  // CHECK: int i = *ip.get();
}

// CHECK-NOT: warning

void Negative() {
  std::unique_ptr<Bar>* u;
  u->get()->Do();

  Fail1().get()->Do();
  Fail2().get()->Do();
  const Bar& b = *Fail1().get();
  (*Fail2().get()).Do();
}
OpenPOWER on IntegriCloud