| 1 |
// RUN: %check_clang_tidy %s bugprone-return-const-ref-from-parameter %t -- -- -fno-delayed-template-parsing |
| 2 |
|
| 3 |
using T = int; |
| 4 |
using TConst = int const; |
| 5 |
using TConstRef = int const&; |
| 6 |
|
| 7 |
template <typename T> |
| 8 |
struct Wrapper { Wrapper(T); }; |
| 9 |
|
| 10 |
template <typename T> |
| 11 |
struct Identity { using type = T; }; |
| 12 |
|
| 13 |
template <typename T> |
| 14 |
struct ConstRef { using type = const T&; }; |
| 15 |
|
| 16 |
namespace invalid { |
| 17 |
|
| 18 |
int const &f1(int const &a) { return a; } |
| 19 |
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: returning a constant reference parameter |
| 20 |
|
| 21 |
int const &f2(T const &a) { return a; } |
| 22 |
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: returning a constant reference parameter |
| 23 |
|
| 24 |
int const &f3(TConstRef a) { return a; } |
| 25 |
// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: returning a constant reference parameter |
| 26 |
|
| 27 |
int const &f4(TConst &a) { return a; } |
| 28 |
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: returning a constant reference parameter |
| 29 |
|
| 30 |
template <typename T> |
| 31 |
const T& tf1(const T &a) { return a; } |
| 32 |
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: returning a constant reference parameter |
| 33 |
|
| 34 |
template <typename T> |
| 35 |
const T& itf1(const T &a) { return a; } |
| 36 |
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: returning a constant reference parameter |
| 37 |
|
| 38 |
template <typename T> |
| 39 |
typename ConstRef<T>::type itf2(const T &a) { return a; } |
| 40 |
// CHECK-MESSAGES: :[[@LINE-1]]:54: warning: returning a constant reference parameter |
| 41 |
|
| 42 |
template <typename T> |
| 43 |
typename ConstRef<T>::type itf3(typename ConstRef<T>::type a) { return a; } |
| 44 |
// CHECK-MESSAGES: :[[@LINE-1]]:72: warning: returning a constant reference parameter |
| 45 |
|
| 46 |
template <typename T> |
| 47 |
const T& itf4(typename ConstRef<T>::type a) { return a; } |
| 48 |
// CHECK-MESSAGES: :[[@LINE-1]]:54: warning: returning a constant reference parameter |
| 49 |
|
| 50 |
void instantiate(const int ¶m, const float ¶mf, int &mut_param, float &mut_paramf) { |
| 51 |
itf1(0); |
| 52 |
itf1(param); |
| 53 |
itf1(paramf); |
| 54 |
itf2(0); |
| 55 |
itf2(param); |
| 56 |
itf2(paramf); |
| 57 |
itf3<int>(0); |
| 58 |
itf3<int>(param); |
| 59 |
itf3<float>(paramf); |
| 60 |
itf4<int>(0); |
| 61 |
itf4<int>(param); |
| 62 |
itf4<float>(paramf); |
| 63 |
} |
| 64 |
|
| 65 |
struct C { |
| 66 |
const C& foo(const C&c) { return c; } |
| 67 |
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: returning a constant reference parameter |
| 68 |
}; |
| 69 |
|
| 70 |
} // namespace invalid |
| 71 |
|
| 72 |
namespace false_negative_because_dependent_and_not_instantiated { |
| 73 |
template <typename T> |
| 74 |
typename ConstRef<T>::type tf2(const T &a) { return a; } |
| 75 |
|
| 76 |
template <typename T> |
| 77 |
typename ConstRef<T>::type tf3(typename ConstRef<T>::type a) { return a; } |
| 78 |
|
| 79 |
template <typename T> |
| 80 |
const T& tf4(typename ConstRef<T>::type a) { return a; } |
| 81 |
} // false_negative_because_dependent_and_not_instantiated |
| 82 |
|
| 83 |
namespace valid { |
| 84 |
|
| 85 |
int const &f1(int &a) { return a; } |
| 86 |
|
| 87 |
int const &f2(int &&a) { return a; } |
| 88 |
|
| 89 |
int f1(int const &a) { return a; } |
| 90 |
|
| 91 |
template <typename T> |
| 92 |
T tf1(T a) { return a; } |
| 93 |
|
| 94 |
template <typename T> |
| 95 |
T tf2(const T a) { return a; } |
| 96 |
|
| 97 |
template <typename T> |
| 98 |
T tf3(const T &a) { return a; } |
| 99 |
|
| 100 |
template <typename T> |
| 101 |
Identity<T>::type tf4(const T &a) { return a; } |
| 102 |
|
| 103 |
template <typename T> |
| 104 |
T itf1(T a) { return a; } |
| 105 |
|
| 106 |
template <typename T> |
| 107 |
T itf2(const T a) { return a; } |
| 108 |
|
| 109 |
template <typename T> |
| 110 |
T itf3(const T &a) { return a; } |
| 111 |
|
| 112 |
template <typename T> |
| 113 |
Wrapper<T> itf4(const T& a) { return a; } |
| 114 |
|
| 115 |
template <typename T> |
| 116 |
const T& itf5(T& a) { return a; } |
| 117 |
|
| 118 |
template <typename T> |
| 119 |
T itf6(T& a) { return a; } |
| 120 |
|
| 121 |
void instantiate(const int ¶m, const float ¶mf, int &mut_param, float &mut_paramf) { |
| 122 |
itf1(0); |
| 123 |
itf1(param); |
| 124 |
itf1(paramf); |
| 125 |
itf2(0); |
| 126 |
itf2(param); |
| 127 |
itf2(paramf); |
| 128 |
itf3(0); |
| 129 |
itf3(param); |
| 130 |
itf3(paramf); |
| 131 |
itf2(0); |
| 132 |
itf2(param); |
| 133 |
itf2(paramf); |
| 134 |
itf3(0); |
| 135 |
itf3(param); |
| 136 |
itf3(paramf); |
| 137 |
itf4(param); |
| 138 |
itf4(paramf); |
| 139 |
itf5(mut_param); |
| 140 |
itf5(mut_paramf); |
| 141 |
itf6(mut_param); |
| 142 |
itf6(mut_paramf); |
| 143 |
} |
| 144 |
|
| 145 |
} // namespace valid |
| 146 |
|
| 147 |
namespace overload { |
| 148 |
|
| 149 |
int const &overload_base(int const &a) { return a; } |
| 150 |
int const &overload_base(int &&a); |
| 151 |
|
| 152 |
int const &overload_ret_type(int const &a) { return a; } |
| 153 |
void overload_ret_type(int &&a); |
| 154 |
|
| 155 |
int const &overload_params1(int p1, int const &a) { return a; } |
| 156 |
int const & overload_params1(int p1, int &&a); |
| 157 |
|
| 158 |
int const &overload_params2(int p1, int const &a, int p2) { return a; } |
| 159 |
int const &overload_params2(int p1, int &&a, int p2); |
| 160 |
|
| 161 |
int const &overload_params3(T p1, int const &a, int p2) { return a; } |
| 162 |
int const &overload_params3(int p1, int &&a, T p2); |
| 163 |
|
| 164 |
int const &overload_params_const(int p1, int const &a, int const p2) { return a; } |
| 165 |
int const &overload_params_const(int const p1, int &&a, int p2); |
| 166 |
|
| 167 |
int const &overload_params_difference1(int p1, int const &a, int p2) { return a; } |
| 168 |
// CHECK-MESSAGES: :[[@LINE-1]]:79: warning: returning a constant reference parameter |
| 169 |
int const &overload_params_difference1(long p1, int &&a, int p2); |
| 170 |
|
| 171 |
int const &overload_params_difference2(int p1, int const &a, int p2) { return a; } |
| 172 |
// CHECK-MESSAGES: :[[@LINE-1]]:79: warning: returning a constant reference parameter |
| 173 |
int const &overload_params_difference2(int p1, int &&a, long p2); |
| 174 |
|
| 175 |
int const &overload_params_difference3(int p1, int const &a, int p2) { return a; } |
| 176 |
// CHECK-MESSAGES: :[[@LINE-1]]:79: warning: returning a constant reference parameter |
| 177 |
int const &overload_params_difference3(int p1, long &&a, int p2); |
| 178 |
|
| 179 |
} // namespace overload |