Skip to content

Commit 976fea0

Browse files
gruenichrwgk
andauthored
Fix Clazy warnings (#4988)
* stl.h: Use C++11 range-loops with const reference This saves copy-ctor and dtor for non-trivial types by value Found by clazy (range-loop-reference) * test_smart_ptr.cpp cleanup Introduce `pointer_set<T>` boostorg/unordered#139 > Based on the standard, the first move should leave a in a "valid but unspecified state"; --------- Co-authored-by: Ralf W. Grosse-Kunstleve <[email protected]>
1 parent eeac2f4 commit 976fea0

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

include/pybind11/stl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ struct list_caster {
172172
auto s = reinterpret_borrow<sequence>(src);
173173
value.clear();
174174
reserve_maybe(s, &value);
175-
for (auto it : s) {
175+
for (const auto &it : s) {
176176
value_conv conv;
177177
if (!conv.load(it, convert)) {
178178
return false;
@@ -247,7 +247,7 @@ struct array_caster {
247247
return false;
248248
}
249249
size_t ctr = 0;
250-
for (auto it : l) {
250+
for (const auto &it : l) {
251251
value_conv conv;
252252
if (!conv.load(it, convert)) {
253253
return false;

tests/test_smart_ptr.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,57 +103,60 @@ class MyObject3 : public std::enable_shared_from_this<MyObject3> {
103103
int value;
104104
};
105105

106+
template <typename T>
107+
std::unordered_set<T *> &pointer_set() {
108+
// https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
109+
static auto singleton = new std::unordered_set<T *>();
110+
return *singleton;
111+
}
112+
106113
// test_unique_nodelete
107114
// Object with a private destructor
108-
class MyObject4;
109-
std::unordered_set<MyObject4 *> myobject4_instances;
110115
class MyObject4 {
111116
public:
112117
explicit MyObject4(int value) : value{value} {
113118
print_created(this);
114-
myobject4_instances.insert(this);
119+
pointer_set<MyObject4>().insert(this);
115120
}
116121
int value;
117122

118123
static void cleanupAllInstances() {
119-
auto tmp = std::move(myobject4_instances);
120-
myobject4_instances.clear();
124+
auto tmp = std::move(pointer_set<MyObject4>());
125+
pointer_set<MyObject4>().clear();
121126
for (auto *o : tmp) {
122127
delete o;
123128
}
124129
}
125130

126131
private:
127132
~MyObject4() {
128-
myobject4_instances.erase(this);
133+
pointer_set<MyObject4>().erase(this);
129134
print_destroyed(this);
130135
}
131136
};
132137

133138
// test_unique_deleter
134139
// Object with std::unique_ptr<T, D> where D is not matching the base class
135140
// Object with a protected destructor
136-
class MyObject4a;
137-
std::unordered_set<MyObject4a *> myobject4a_instances;
138141
class MyObject4a {
139142
public:
140143
explicit MyObject4a(int i) : value{i} {
141144
print_created(this);
142-
myobject4a_instances.insert(this);
145+
pointer_set<MyObject4a>().insert(this);
143146
};
144147
int value;
145148

146149
static void cleanupAllInstances() {
147-
auto tmp = std::move(myobject4a_instances);
148-
myobject4a_instances.clear();
150+
auto tmp = std::move(pointer_set<MyObject4a>());
151+
pointer_set<MyObject4a>().clear();
149152
for (auto *o : tmp) {
150153
delete o;
151154
}
152155
}
153156

154157
protected:
155158
virtual ~MyObject4a() {
156-
myobject4a_instances.erase(this);
159+
pointer_set<MyObject4a>().erase(this);
157160
print_destroyed(this);
158161
}
159162
};

0 commit comments

Comments
 (0)