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
|
// RUN: %check_clang_tidy %s cppcoreguidelines-avoid-const-or-ref-data-members %t
namespace std {
template <typename T>
struct unique_ptr {};
template <typename T>
struct shared_ptr {};
} // namespace std
namespace gsl {
template <typename T>
struct not_null {};
} // namespace gsl
struct Ok {
int i;
int *p;
const int *pc;
std::unique_ptr<int> up;
std::shared_ptr<int> sp;
gsl::not_null<int> n;
};
struct ConstMember {
const int c;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: member 'c' of type 'const int' is const qualified [cppcoreguidelines-avoid-const-or-ref-data-members]
};
struct LvalueRefMember {
int &lr;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'lr' of type 'int &' is a reference
};
struct ConstRefMember {
const int &cr;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: member 'cr' of type 'const int &' is a reference
};
struct RvalueRefMember {
int &&rr;
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: member 'rr' of type 'int &&' is a reference
};
struct ConstAndRefMembers {
const int c;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: member 'c' of type 'const int' is const qualified
int &lr;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'lr' of type 'int &' is a reference
const int &cr;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: member 'cr' of type 'const int &' is a reference
int &&rr;
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: member 'rr' of type 'int &&' is a reference
};
struct Foo {};
struct Ok2 {
Foo i;
Foo *p;
const Foo *pc;
std::unique_ptr<Foo> up;
std::shared_ptr<Foo> sp;
gsl::not_null<Foo> n;
};
struct ConstMember2 {
const Foo c;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: member 'c' of type 'const Foo' is const qualified
};
struct LvalueRefMember2 {
Foo &lr;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'lr' of type 'Foo &' is a reference
};
struct ConstRefMember2 {
const Foo &cr;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: member 'cr' of type 'const Foo &' is a reference
};
struct RvalueRefMember2 {
Foo &&rr;
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: member 'rr' of type 'Foo &&' is a reference
};
struct ConstAndRefMembers2 {
const Foo c;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: member 'c' of type 'const Foo' is const qualified
Foo &lr;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: member 'lr' of type 'Foo &' is a reference
const Foo &cr;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: member 'cr' of type 'const Foo &' is a reference
Foo &&rr;
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: member 'rr' of type 'Foo &&' is a reference
};
using ConstType = const int;
using RefType = int &;
using ConstRefType = const int &;
using RefRefType = int &&;
struct WithAlias {
ConstType c;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: member 'c' of type 'ConstType' (aka 'const int') is const qualified
RefType lr;
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: member 'lr' of type 'RefType' (aka 'int &') is a reference
ConstRefType cr;
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: member 'cr' of type 'ConstRefType' (aka 'const int &') is a reference
RefRefType rr;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: member 'rr' of type 'RefRefType' (aka 'int &&') is a reference
};
template <int N>
using Array = int[N];
struct ConstArrayMember {
const Array<1> c;
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: member 'c' of type 'const Array<1>' (aka 'const int[1]') is const qualified
};
struct LvalueRefArrayMember {
Array<2> &lr;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: member 'lr' of type 'Array<2> &' (aka 'int (&)[2]') is a reference
};
struct ConstLvalueRefArrayMember {
const Array<3> &cr;
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: member 'cr' of type 'const Array<3> &' (aka 'const int (&)[3]') is a reference
};
struct RvalueRefArrayMember {
Array<4> &&rr;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: member 'rr' of type 'Array<4> &&' (aka 'int (&&)[4]') is a reference
};
template <typename T>
struct TemplatedOk {
T t;
};
template <typename T>
struct TemplatedConst {
T t;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: member 't' of type 'const int' is const qualified
};
template <typename T>
struct TemplatedConstRef {
T t;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: member 't' of type 'const int &' is a reference
};
template <typename T>
struct TemplatedRefRef {
T t;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: member 't' of type 'int &&' is a reference
};
template <typename T>
struct TemplatedRef {
T t;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: member 't' of type 'int &' is a reference
};
TemplatedOk<int> t1{};
TemplatedConst<const int> t2{123};
TemplatedConstRef<const int &> t3{123};
TemplatedRefRef<int &&> t4{123};
TemplatedRef<int &> t5{t1.t};
// Lambdas capturing const or ref members should not trigger warnings
void lambdas()
{
int x1{123};
const int x2{123};
const int& x3{123};
int&& x4{123};
int& x5{x1};
auto v1 = [x1]{};
auto v2 = [x2]{};
auto v3 = [x3]{};
auto v4 = [x4]{};
auto v5 = [x5]{};
auto r1 = [&x1]{};
auto r2 = [&x2]{};
auto r3 = [&x3]{};
auto r4 = [&x4]{};
auto r5 = [&x5]{};
auto iv = [=]{
auto c1 = x1;
auto c2 = x2;
auto c3 = x3;
auto c4 = x4;
auto c5 = x5;
};
auto ir = [&]{
auto c1 = x1;
auto c2 = x2;
auto c3 = x3;
auto c4 = x4;
auto c5 = x5;
};
}
|