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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
// RUN: %check_clang_tidy %s bugprone-return-const-ref-from-parameter %t -- -- -fno-delayed-template-parsing
using T = int;
using TConst = int const;
using TConstRef = int const&;
template <typename T>
struct Wrapper { Wrapper(T); };
template <typename T>
struct Identity { using type = T; };
template <typename T>
struct ConstRef { using type = const T&; };
namespace invalid {
int const &f1(int const &a) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: returning a constant reference parameter
int const &f2(T const &a) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: returning a constant reference parameter
int const &f3(TConstRef a) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: returning a constant reference parameter
int const &f4(TConst &a) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: returning a constant reference parameter
int const &f5(TConst &a) { return true ? a : a; }
// CHECK-MESSAGES: :[[@LINE-1]]:42: warning: returning a constant reference parameter
// CHECK-MESSAGES: :[[@LINE-2]]:46: warning: returning a constant reference parameter
template <typename T>
const T& tf1(const T &a) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: returning a constant reference parameter
template <typename T>
const T& itf1(const T &a) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: returning a constant reference parameter
template <typename T>
typename ConstRef<T>::type itf2(const T &a) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:54: warning: returning a constant reference parameter
template <typename T>
typename ConstRef<T>::type itf3(typename ConstRef<T>::type a) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:72: warning: returning a constant reference parameter
template <typename T>
const T& itf4(typename ConstRef<T>::type a) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:54: warning: returning a constant reference parameter
template <typename T>
const T& itf5(const T &a) { return true ? a : a; }
// CHECK-MESSAGES: :[[@LINE-1]]:43: warning: returning a constant reference parameter
// CHECK-MESSAGES: :[[@LINE-2]]:47: warning: returning a constant reference parameter
void instantiate(const int ¶m, const float ¶mf, int &mut_param, float &mut_paramf) {
itf1(0);
itf1(param);
itf1(paramf);
itf2(0);
itf2(param);
itf2(paramf);
itf3<int>(0);
itf3<int>(param);
itf3<float>(paramf);
itf4<int>(0);
itf4<int>(param);
itf4<float>(paramf);
}
struct C {
const C& foo(const C&c) { return c; }
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: returning a constant reference parameter
};
const auto Lf1 = [](const T& t) -> const T& { return t; };
// CHECK-MESSAGES: :[[@LINE-1]]:54: warning: returning a constant reference parameter
} // namespace invalid
namespace false_negative_because_dependent_and_not_instantiated {
template <typename T>
typename ConstRef<T>::type tf2(const T &a) { return a; }
template <typename T>
typename ConstRef<T>::type tf3(typename ConstRef<T>::type a) { return a; }
template <typename T>
const T& tf4(typename ConstRef<T>::type a) { return a; }
} // false_negative_because_dependent_and_not_instantiated
namespace valid {
int const &f1(int &a) { return a; }
int const &f2(int &&a) { return a; }
int f1(int const &a) { return a; }
template <typename T>
T tf1(T a) { return a; }
template <typename T>
T tf2(const T a) { return a; }
template <typename T>
T tf3(const T &a) { return a; }
template <typename T>
Identity<T>::type tf4(const T &a) { return a; }
template <typename T>
T itf1(T a) { return a; }
template <typename T>
T itf2(const T a) { return a; }
template <typename T>
T itf3(const T &a) { return a; }
template <typename T>
Wrapper<T> itf4(const T& a) { return a; }
template <typename T>
const T& itf5(T& a) { return a; }
template <typename T>
T itf6(T& a) { return a; }
void instantiate(const int ¶m, const float ¶mf, int &mut_param, float &mut_paramf) {
itf1(0);
itf1(param);
itf1(paramf);
itf2(0);
itf2(param);
itf2(paramf);
itf3(0);
itf3(param);
itf3(paramf);
itf2(0);
itf2(param);
itf2(paramf);
itf3(0);
itf3(param);
itf3(paramf);
itf4(param);
itf4(paramf);
itf5(mut_param);
itf5(mut_paramf);
itf6(mut_param);
itf6(mut_paramf);
}
template<class T>
void f(const T& t) {
const auto get = [&t] -> const T& { return t; };
return T{};
}
const auto Lf1 = [](T& t) -> const T& { return t; };
} // namespace valid
namespace overload {
int const &overload_base(int const &a) { return a; }
int const &overload_base(int &&a);
int const &overload_ret_type(int const &a) { return a; }
void overload_ret_type(int &&a);
int const &overload_params1(int p1, int const &a) { return a; }
int const & overload_params1(int p1, int &&a);
int const &overload_params2(int p1, int const &a, int p2) { return a; }
int const &overload_params2(int p1, int &&a, int p2);
int const &overload_params3(T p1, int const &a, int p2) { return a; }
int const &overload_params3(int p1, int &&a, T p2);
int const &overload_params_const(int p1, int const &a, int const p2) { return a; }
int const &overload_params_const(int const p1, int &&a, int p2);
int const &overload_params_difference1(int p1, int const &a, int p2) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:79: warning: returning a constant reference parameter
int const &overload_params_difference1(long p1, int &&a, int p2);
int const &overload_params_difference2(int p1, int const &a, int p2) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:79: warning: returning a constant reference parameter
int const &overload_params_difference2(int p1, int &&a, long p2);
int const &overload_params_difference3(int p1, int const &a, int p2) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:79: warning: returning a constant reference parameter
int const &overload_params_difference3(int p1, long &&a, int p2);
} // namespace overload
namespace gh117696 {
namespace use_lifetime_bound_attr {
int const &f(int const &a [[clang::lifetimebound]]) { return a; }
} // namespace use_lifetime_bound_attr
} // namespace gh117696
namespace lambda {
using T = const int &;
using K = const float &;
T inner_valid_lambda(T a) {
[&]() -> T { return a; };
return a;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: returning a constant reference parameter
}
T inner_invalid_lambda(T a) {
[&](T a) -> T { return a; };
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: returning a constant reference parameter
return a;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: returning a constant reference parameter
}
T inner_invalid_lambda2(T a) {
[&](K a) -> K { return a; };
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: returning a constant reference parameter
return a;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: returning a constant reference parameter
}
} // namespace lambda
|