Skip to content

Commit 274b014

Browse files
committed
Merge branch 'master' into smart_holder
2 parents 29fafcc + 3b30b0a commit 274b014

16 files changed

+52
-33
lines changed

.clang-tidy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,30 @@ FormatStyle: file
22

33
Checks: '
44
llvm-namespace-comment,
5+
misc-misplaced-const,
6+
misc-static-assert,
7+
misc-uniqueptr-reset-release,
58
modernize-avoid-bind,
69
modernize-replace-auto-ptr,
710
modernize-replace-disallow-copy-and-assign-macro,
811
modernize-shrink-to-fit,
912
modernize-use-auto,
13+
modernize-use-bool-literals,
1014
modernize-use-equals-default,
1115
modernize-use-equals-delete,
16+
modernize-use-default-member-init,
17+
modernize-use-noexcept,
1218
modernize-use-emplace,
1319
modernize-use-override,
1420
modernize-use-using,
1521
readability-container-size-empty,
22+
readability-make-member-function-const,
23+
readability-redundant-function-ptr-dereference,
24+
readability-redundant-smartptr-get,
25+
readability-redundant-string-cstr,
26+
readability-simplify-subscript-expr,
27+
readability-string-compare,
28+
readability-uniqueptr-delete-release,
1629
'
1730

1831
CheckOptions:

include/pybind11/detail/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ struct instance {
480480
void allocate_layout();
481481

482482
/// Destroys/deallocates all of the above
483-
void deallocate_layout();
483+
void deallocate_layout() const;
484484

485485
/// Returns the value_and_holder wrapper for the given type (or the first, if `find_type`
486486
/// omitted). Returns a default-constructed (with `.inst = nullptr`) object on failure if

include/pybind11/detail/descr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ PYBIND11_NAMESPACE_BEGIN(detail)
2323
/* Concatenate type signatures at compile time */
2424
template <size_t N, typename... Ts>
2525
struct descr {
26-
char text[N + 1];
26+
char text[N + 1]{'\0'};
2727

28-
constexpr descr() : text{'\0'} { }
28+
constexpr descr() = default;
2929
constexpr descr(char const (&s)[N+1]) : descr(s, make_index_sequence<N>()) { }
3030

3131
template <size_t... Is>

include/pybind11/detail/type_caster_base.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ struct value_and_holder {
241241
? inst->simple_holder_constructed
242242
: inst->nonsimple.status[index] & instance::status_holder_constructed;
243243
}
244-
void set_holder_constructed(bool v = true) {
244+
void set_holder_constructed(bool v = true) const {
245245
if (inst->simple_layout)
246246
inst->simple_holder_constructed = v;
247247
else if (v)
@@ -254,7 +254,7 @@ struct value_and_holder {
254254
? inst->simple_instance_registered
255255
: inst->nonsimple.status[index] & instance::status_instance_registered;
256256
}
257-
void set_instance_registered(bool v = true) {
257+
void set_instance_registered(bool v = true) const {
258258
if (inst->simple_layout)
259259
inst->simple_instance_registered = v;
260260
else if (v)
@@ -397,7 +397,7 @@ PYBIND11_NOINLINE inline void instance::allocate_layout() {
397397
owned = true;
398398
}
399399

400-
PYBIND11_NOINLINE inline void instance::deallocate_layout() {
400+
PYBIND11_NOINLINE inline void instance::deallocate_layout() const {
401401
if (!simple_layout)
402402
PyMem_Free(nonsimple.values_and_holders);
403403
}

include/pybind11/numpy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ class common_iterator {
12911291
using value_type = container_type::value_type;
12921292
using size_type = container_type::size_type;
12931293

1294-
common_iterator() : p_ptr(0), m_strides() {}
1294+
common_iterator() : m_strides() {}
12951295

12961296
common_iterator(void* ptr, const container_type& strides, const container_type& shape)
12971297
: p_ptr(reinterpret_cast<char*>(ptr)), m_strides(strides.size()) {
@@ -1312,7 +1312,7 @@ class common_iterator {
13121312
}
13131313

13141314
private:
1315-
char* p_ptr;
1315+
char *p_ptr{0};
13161316
container_type m_strides;
13171317
};
13181318

include/pybind11/pybind11.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ class cpp_function : public function {
369369
rec->def = new PyMethodDef();
370370
std::memset(rec->def, 0, sizeof(PyMethodDef));
371371
rec->def->ml_name = rec->name;
372-
rec->def->ml_meth = reinterpret_cast<PyCFunction>(reinterpret_cast<void (*) (void)>(*dispatcher));
372+
rec->def->ml_meth
373+
= reinterpret_cast<PyCFunction>(reinterpret_cast<void (*)(void)>(dispatcher));
373374
rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
374375

375376
capsule rec_capsule(unique_rec.release(), [](void *ptr) {

tests/local_bindings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Pet {
5656
public:
5757
Pet(std::string name) : name_(name) {}
5858
std::string name_;
59-
const std::string &name() { return name_; }
59+
const std::string &name() const { return name_; }
6060
};
6161
} // namespace pets
6262

tests/test_constants_and_functions.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw byte strings
2+
tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw
3+
byte strings
34
45
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
56
@@ -60,6 +61,7 @@ int f3(int x) noexcept(false) { return x+3; }
6061
# pragma GCC diagnostic push
6162
# pragma GCC diagnostic ignored "-Wdeprecated"
6263
#endif
64+
// NOLINTNEXTLINE(modernize-use-noexcept)
6365
int f4(int x) throw() { return x+4; } // Deprecated equivalent to noexcept(true)
6466
#if defined(__GNUG__) && !defined(__INTEL_COMPILER)
6567
# pragma GCC diagnostic pop
@@ -75,8 +77,10 @@ struct C {
7577
# pragma GCC diagnostic push
7678
# pragma GCC diagnostic ignored "-Wdeprecated"
7779
#endif
78-
int m7(int x) throw() { return x-7; }
79-
int m8(int x) const throw() { return x-8; }
80+
// NOLINTNEXTLINE(modernize-use-noexcept)
81+
int m7(int x) throw() { return x - 7; }
82+
// NOLINTNEXTLINE(modernize-use-noexcept)
83+
int m8(int x) const throw() { return x - 8; }
8084
#if defined(__GNUG__) && !defined(__INTEL_COMPILER)
8185
# pragma GCC diagnostic pop
8286
#endif

tests/test_factory_constructors.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class TestFactory6 {
7777
TestFactory6(const TestFactory6 &f) { print_copy_created(this); value = f.value; alias = f.alias; }
7878
virtual ~TestFactory6() { print_destroyed(this); }
7979
virtual int get() { return value; }
80-
bool has_alias() { return alias; }
80+
bool has_alias() const { return alias; }
8181
};
8282
class PyTF6 : public TestFactory6 {
8383
public:
@@ -102,7 +102,7 @@ class TestFactory7 {
102102
TestFactory7(const TestFactory7 &f) { print_copy_created(this); value = f.value; alias = f.alias; }
103103
virtual ~TestFactory7() { print_destroyed(this); }
104104
virtual int get() { return value; }
105-
bool has_alias() { return alias; }
105+
bool has_alias() const { return alias; }
106106
};
107107
class PyTF7 : public TestFactory7 {
108108
public:

tests/test_iostream.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void noisy_funct_dual(std::string msg, std::string emsg) {
3434
// simply repeatedly write to std::cerr until stopped
3535
// redirect is called at some point to test the safety of scoped_estream_redirect
3636
struct TestThread {
37-
TestThread() : t_{nullptr}, stop_{false} {
37+
TestThread() : stop_{false} {
3838
auto thread_f = [this] {
3939
while (!stop_) {
4040
std::cout << "x" << std::flush;
@@ -49,7 +49,7 @@ struct TestThread {
4949

5050
void stop() { stop_ = true; }
5151

52-
void join() {
52+
void join() const {
5353
py::gil_scoped_release gil_lock;
5454
t_->join();
5555
}
@@ -59,7 +59,7 @@ struct TestThread {
5959
std::this_thread::sleep_for(std::chrono::milliseconds(50));
6060
}
6161

62-
std::thread * t_;
62+
std::thread *t_{nullptr};
6363
std::atomic<bool> stop_;
6464
};
6565

tests/test_methods_and_attributes.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ class ExampleMandA {
2525
ExampleMandA(ExampleMandA &&e) : value(e.value) { print_move_created(this); }
2626
~ExampleMandA() { print_destroyed(this); }
2727

28-
std::string toString() {
29-
return "ExampleMandA[value=" + std::to_string(value) + "]";
30-
}
28+
std::string toString() const { return "ExampleMandA[value=" + std::to_string(value) + "]"; }
3129

3230
void operator=(const ExampleMandA &e) { print_copy_assigned(this); value = e.value; }
3331
void operator=(ExampleMandA &&e) { print_move_assigned(this); value = e.value; }
@@ -48,13 +46,13 @@ class ExampleMandA {
4846

4947
ExampleMandA self1() { return *this; } // return by value
5048
ExampleMandA &self2() { return *this; } // return by reference
51-
const ExampleMandA &self3() { return *this; } // return by const reference
49+
const ExampleMandA &self3() const { return *this; } // return by const reference
5250
ExampleMandA *self4() { return this; } // return by pointer
53-
const ExampleMandA *self5() { return this; } // return by const pointer
51+
const ExampleMandA *self5() const { return this; } // return by const pointer
5452

55-
int internal1() { return value; } // return by value
53+
int internal1() const { return value; } // return by value
5654
int &internal2() { return value; } // return by reference
57-
const int &internal3() { return value; } // return by const reference
55+
const int &internal3() const { return value; } // return by const reference
5856
int *internal4() { return &value; } // return by pointer
5957
const int *internal5() { return &value; } // return by const pointer
6058

tests/test_modules.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ TEST_SUBMODULE(modules, m) {
2424
~A() { print_destroyed(this); }
2525
A(const A&) { print_copy_created(this); }
2626
A& operator=(const A &copy) { print_copy_assigned(this); v = copy.v; return *this; }
27-
std::string toString() { return "A[" + std::to_string(v) + "]"; }
27+
std::string toString() const { return "A[" + std::to_string(v) + "]"; }
28+
2829
private:
2930
int v;
3031
};

tests/test_multiple_inheritance.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ int VanillaStaticMix2::static_value = 12;
4848
// test_multiple_inheritance_virtbase
4949
struct Base1a {
5050
Base1a(int i) : i(i) { }
51-
int foo() { return i; }
51+
int foo() const { return i; }
5252
int i;
5353
};
5454
struct Base2a {
5555
Base2a(int i) : i(i) { }
56-
int bar() { return i; }
56+
int bar() const { return i; }
5757
int i;
5858
};
5959
struct Base12a : Base1a, Base2a {
@@ -78,7 +78,7 @@ TEST_SUBMODULE(multiple_inheritance, m) {
7878
// test_multiple_inheritance_mix2
7979
struct Base1 {
8080
Base1(int i) : i(i) { }
81-
int foo() { return i; }
81+
int foo() const { return i; }
8282
int i;
8383
};
8484
py::class_<Base1> b1(m, "Base1");
@@ -87,7 +87,7 @@ TEST_SUBMODULE(multiple_inheritance, m) {
8787

8888
struct Base2 {
8989
Base2(int i) : i(i) { }
90-
int bar() { return i; }
90+
int bar() const { return i; }
9191
int i;
9292
};
9393
py::class_<Base2> b2(m, "Base2");

tests/test_numpy_vectorize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ TEST_SUBMODULE(numpy_vectorize, m) {
6262
// test_method_vectorization
6363
struct VectorizeTestClass {
6464
VectorizeTestClass(int v) : value{v} {};
65-
float method(int x, float y) { return y + (float) (x + value); }
65+
float method(int x, float y) const { return y + (float) (x + value); }
6666
int value = 0;
6767
};
6868
py::class_<VectorizeTestClass> vtc(m, "VectorizeTestClass");

tests/test_smart_ptr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ struct ElementBase {
240240

241241
struct ElementA : ElementBase {
242242
ElementA(int v) : v(v) { }
243-
int value() { return v; }
243+
int value() const { return v; }
244244
int v;
245245
};
246246

tests/test_stl_binders.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,7 @@ TEST_SUBMODULE(stl_binders, m) {
125125
PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
126126
py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
127127
py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
128-
m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
128+
m.def("get_vectorstruct", [] {
129+
return std::vector<VStruct>{{false, 5, 3.0, true}, {true, 30, -1e4, false}};
130+
});
129131
}

0 commit comments

Comments
 (0)