Skip to content

Commit f7b0ac7

Browse files
committed
Systematically resolving clang-tidy errors in test_numpy_array.cpp, test_stl.cpp, by converting all pass-by-value to pass-by-const-ref.
1 parent fffdf3e commit f7b0ac7

File tree

2 files changed

+45
-90
lines changed

2 files changed

+45
-90
lines changed

tests/test_numpy_array.cpp

Lines changed: 41 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,7 @@ TEST_SUBMODULE(numpy_array, sm) {
226226
return py::isinstance<py::array>(std::move(yes))
227227
&& !py::isinstance<py::array>(std::move(no));
228228
});
229-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
230-
sm.def("isinstance_typed", [](py::object o) {
229+
sm.def("isinstance_typed", [](const py::object &o) {
231230
return py::isinstance<py::array_t<double>>(o) && !py::isinstance<py::array_t<int>>(o);
232231
});
233232

@@ -248,66 +247,47 @@ TEST_SUBMODULE(numpy_array, sm) {
248247
});
249248

250249
// test_overload_resolution
251-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
252-
sm.def("overloaded", [](py::array_t<double>) { return "double"; });
253-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
254-
sm.def("overloaded", [](py::array_t<float>) { return "float"; });
255-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
256-
sm.def("overloaded", [](py::array_t<int>) { return "int"; });
257-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
258-
sm.def("overloaded", [](py::array_t<unsigned short>) { return "unsigned short"; });
259-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
260-
sm.def("overloaded", [](py::array_t<long long>) { return "long long"; });
261-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
262-
sm.def("overloaded", [](py::array_t<std::complex<double>>) { return "double complex"; });
263-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
264-
sm.def("overloaded", [](py::array_t<std::complex<float>>) { return "float complex"; });
265-
266-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
267-
sm.def("overloaded2", [](py::array_t<std::complex<double>>) { return "double complex"; });
268-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
269-
sm.def("overloaded2", [](py::array_t<double>) { return "double"; });
270-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
271-
sm.def("overloaded2", [](py::array_t<std::complex<float>>) { return "float complex"; });
272-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
273-
sm.def("overloaded2", [](py::array_t<float>) { return "float"; });
250+
sm.def("overloaded", [](const py::array_t<double> &) { return "double"; });
251+
sm.def("overloaded", [](const py::array_t<float> &) { return "float"; });
252+
sm.def("overloaded", [](const py::array_t<int> &) { return "int"; });
253+
sm.def("overloaded", [](const py::array_t<unsigned short> &) { return "unsigned short"; });
254+
sm.def("overloaded", [](const py::array_t<long long> &) { return "long long"; });
255+
sm.def("overloaded",
256+
[](const py::array_t<std::complex<double>> &) { return "double complex"; });
257+
sm.def("overloaded", [](const py::array_t<std::complex<float>> &) { return "float complex"; });
258+
259+
sm.def("overloaded2",
260+
[](const py::array_t<std::complex<double>> &) { return "double complex"; });
261+
sm.def("overloaded2", [](const py::array_t<double> &) { return "double"; });
262+
sm.def("overloaded2",
263+
[](const py::array_t<std::complex<float>> &) { return "float complex"; });
264+
sm.def("overloaded2", [](const py::array_t<float> &) { return "float"; });
274265

275266
// [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works.
276267

277268
// Only accept the exact types:
278269
sm.def(
279-
"overloaded3",
280-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
281-
[](py::array_t<int>) { return "int"; },
282-
py::arg{}.noconvert());
270+
"overloaded3", [](const py::array_t<int> &) { return "int"; }, py::arg{}.noconvert());
283271
sm.def(
284272
"overloaded3",
285-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
286-
[](py::array_t<double>) { return "double"; },
273+
[](const py::array_t<double> &) { return "double"; },
287274
py::arg{}.noconvert());
288275

289276
// Make sure we don't do unsafe coercion (e.g. float to int) when not using forcecast, but
290277
// rather that float gets converted via the safe (conversion to double) overload:
291-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
292-
sm.def("overloaded4", [](py::array_t<long long, 0>) { return "long long"; });
293-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
294-
sm.def("overloaded4", [](py::array_t<double, 0>) { return "double"; });
278+
sm.def("overloaded4", [](const py::array_t<long long, 0> &) { return "long long"; });
279+
sm.def("overloaded4", [](const py::array_t<double, 0> &) { return "double"; });
295280

296281
// But we do allow conversion to int if forcecast is enabled (but only if no overload matches
297282
// without conversion)
298-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
299-
sm.def("overloaded5", [](py::array_t<unsigned int>) { return "unsigned int"; });
300-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
301-
sm.def("overloaded5", [](py::array_t<double>) { return "double"; });
283+
sm.def("overloaded5", [](const py::array_t<unsigned int> &) { return "unsigned int"; });
284+
sm.def("overloaded5", [](const py::array_t<double> &) { return "double"; });
302285

303286
// test_greedy_string_overload
304287
// Issue 685: ndarray shouldn't go to std::string overload
305-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
306-
sm.def("issue685", [](std::string) { return "string"; });
307-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
308-
sm.def("issue685", [](py::array) { return "array"; });
309-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
310-
sm.def("issue685", [](py::object) { return "other"; });
288+
sm.def("issue685", [](const std::string &) { return "string"; });
289+
sm.def("issue685", [](const py::array &) { return "array"; });
290+
sm.def("issue685", [](const py::object &) { return "other"; });
311291

312292
// test_array_unchecked_fixed_dims
313293
sm.def("proxy_add2", [](py::array_t<double> a, double v) {
@@ -430,73 +410,53 @@ TEST_SUBMODULE(numpy_array, sm) {
430410

431411
// test_argument_conversions
432412
sm.def(
433-
"accept_double",
434-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
435-
[](py::array_t<double, 0>) {},
436-
py::arg("a"));
413+
"accept_double", [](const py::array_t<double, 0> &) {}, py::arg("a"));
437414
sm.def(
438415
"accept_double_forcecast",
439-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
440-
[](py::array_t<double, py::array::forcecast>) {},
416+
[](const py::array_t<double, py::array::forcecast> &) {},
441417
py::arg("a"));
442418
sm.def(
443419
"accept_double_c_style",
444-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
445-
[](py::array_t<double, py::array::c_style>) {},
420+
[](const py::array_t<double, py::array::c_style> &) {},
446421
py::arg("a"));
447422
sm.def(
448423
"accept_double_c_style_forcecast",
449-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
450-
[](py::array_t<double, py::array::forcecast | py::array::c_style>) {},
424+
[](const py::array_t<double, py::array::forcecast | py::array::c_style> &) {},
451425
py::arg("a"));
452426
sm.def(
453427
"accept_double_f_style",
454-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
455-
[](py::array_t<double, py::array::f_style>) {},
428+
[](const py::array_t<double, py::array::f_style> &) {},
456429
py::arg("a"));
457430
sm.def(
458431
"accept_double_f_style_forcecast",
459-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
460-
[](py::array_t<double, py::array::forcecast | py::array::f_style>) {},
432+
[](const py::array_t<double, py::array::forcecast | py::array::f_style> &) {},
461433
py::arg("a"));
462434
sm.def(
463-
"accept_double_noconvert",
464-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
465-
[](py::array_t<double, 0>) {},
466-
"a"_a.noconvert());
435+
"accept_double_noconvert", [](const py::array_t<double, 0> &) {}, "a"_a.noconvert());
467436
sm.def(
468437
"accept_double_forcecast_noconvert",
469-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
470-
[](py::array_t<double, py::array::forcecast>) {},
438+
[](const py::array_t<double, py::array::forcecast> &) {},
471439
"a"_a.noconvert());
472440
sm.def(
473441
"accept_double_c_style_noconvert",
474-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
475-
[](py::array_t<double, py::array::c_style>) {},
442+
[](const py::array_t<double, py::array::c_style> &) {},
476443
"a"_a.noconvert());
477444
sm.def(
478445
"accept_double_c_style_forcecast_noconvert",
479-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
480-
[](py::array_t<double, py::array::forcecast | py::array::c_style>) {},
446+
[](const py::array_t<double, py::array::forcecast | py::array::c_style> &) {},
481447
"a"_a.noconvert());
482448
sm.def(
483449
"accept_double_f_style_noconvert",
484-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
485-
[](py::array_t<double, py::array::f_style>) {},
450+
[](const py::array_t<double, py::array::f_style> &) {},
486451
"a"_a.noconvert());
487452
sm.def(
488453
"accept_double_f_style_forcecast_noconvert",
489-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
490-
[](py::array_t<double, py::array::forcecast | py::array::f_style>) {},
454+
[](const py::array_t<double, py::array::forcecast | py::array::f_style> &) {},
491455
"a"_a.noconvert());
492456

493457
// Check that types returns correct npy format descriptor
494-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
495-
sm.def("test_fmt_desc_float", [](py::array_t<float>) {});
496-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
497-
sm.def("test_fmt_desc_double", [](py::array_t<double>) {});
498-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
499-
sm.def("test_fmt_desc_const_float", [](py::array_t<const float>) {});
500-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
501-
sm.def("test_fmt_desc_const_double", [](py::array_t<const double>) {});
458+
sm.def("test_fmt_desc_float", [](const py::array_t<float> &) {});
459+
sm.def("test_fmt_desc_double", [](const py::array_t<double> &) {});
460+
sm.def("test_fmt_desc_const_float", [](const py::array_t<const float> &) {});
461+
sm.def("test_fmt_desc_const_double", [](const py::array_t<const double> &) {});
502462
}

tests/test_stl.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ TEST_SUBMODULE(stl, m) {
207207
}, py::arg_v("x", std::nullopt, "None"));
208208

209209
m.def("nodefer_none_optional", [](std::optional<int>) { return true; });
210-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
211-
m.def("nodefer_none_optional", [](py::none) { return false; });
210+
m.def("nodefer_none_optional", [](const py::none &) { return false; });
212211

213212
using opt_holder = OptionalHolder<std::optional, MoveOutDetector>;
214213
py::class_<opt_holder>(m, "OptionalHolder", "Class with optional member")
@@ -300,14 +299,10 @@ TEST_SUBMODULE(stl, m) {
300299

301300
// #1258: pybind11/stl.h converts string to vector<string>
302301
m.def("func_with_string_or_vector_string_arg_overload",
303-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
304-
[](std::vector<std::string>) { return 1; });
302+
[](const std::vector<std::string> &) { return 1; });
305303
m.def("func_with_string_or_vector_string_arg_overload",
306-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
307-
[](std::list<std::string>) { return 2; });
308-
m.def("func_with_string_or_vector_string_arg_overload",
309-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
310-
[](std::string) { return 3; });
304+
[](const std::list<std::string> &) { return 2; });
305+
m.def("func_with_string_or_vector_string_arg_overload", [](const std::string &) { return 3; });
311306

312307
class Placeholder {
313308
public:

0 commit comments

Comments
 (0)