diff --git a/.clang-tidy b/.clang-tidy index ea50bf39b..72304528a 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,10 +1,12 @@ FormatStyle: file Checks: ' +cppcoreguidelines-init-variables, clang-analyzer-optin.cplusplus.VirtualCall, llvm-namespace-comment, misc-misplaced-const, misc-static-assert, +misc-throw-by-value-catch-by-reference, misc-uniqueptr-reset-release, modernize-avoid-bind, modernize-redundant-void-arg, @@ -33,6 +35,8 @@ readability-uniqueptr-delete-release, ' CheckOptions: +- key: performance-for-range-copy.WarnOnAllAutoCopies + value: true - key: performance-unnecessary-value-param.AllowedTypes value: 'exception_ptr$;' diff --git a/include/pybind11/eval.h b/include/pybind11/eval.h index bf06504a3..33fcdc09d 100644 --- a/include/pybind11/eval.h +++ b/include/pybind11/eval.h @@ -55,7 +55,7 @@ object eval(const str &expr, object global = globals(), object local = object()) this seems to be the only alternative */ std::string buffer = "# -*- coding: utf-8 -*-\n" + (std::string) expr; - int start; + int start = 0; switch (mode) { case eval_expr: start = Py_eval_input; break; case eval_single_statement: start = Py_single_input; break; @@ -107,7 +107,7 @@ object eval_file(str fname, object global = globals(), object local = object()) detail::ensure_builtins_in_globals(global); - int start; + int start = 0; switch (mode) { case eval_expr: start = Py_eval_input; break; case eval_single_statement: start = Py_single_input; break; diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index fb9680dfa..32386a44d 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -490,7 +490,7 @@ inline handle get_function(handle value) { inline PyObject * dict_getitemstring(PyObject *v, const char *key) { #if PY_MAJOR_VERSION >= 3 - PyObject *kv, *rv; + PyObject *kv = nullptr, *rv = nullptr; kv = PyUnicode_FromString(key); if (kv == NULL) { throw error_already_set(); @@ -1016,8 +1016,8 @@ public: if (!temp) pybind11_fail("Unable to extract string contents! (encoding issue)"); } - char *buffer; - ssize_t length; + char *buffer = nullptr; + ssize_t length = 0; if (PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length)) pybind11_fail("Unable to extract string contents! (invalid type)"); return std::string(buffer, (size_t) length); @@ -1072,8 +1072,8 @@ public: explicit bytes(const pybind11::str &s); operator std::string() const { - char *buffer; - ssize_t length; + char *buffer = nullptr; + ssize_t length = 0; if (PYBIND11_BYTES_AS_STRING_AND_SIZE(m_ptr, &buffer, &length)) pybind11_fail("Unable to extract bytes contents!"); return std::string(buffer, (size_t) length); @@ -1090,8 +1090,8 @@ inline bytes::bytes(const pybind11::str &s) { if (!temp) pybind11_fail("Unable to extract string contents! (encoding issue)"); } - char *buffer; - ssize_t length; + char *buffer = nullptr; + ssize_t length = 0; if (PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length)) pybind11_fail("Unable to extract string contents! (invalid type)"); auto obj = reinterpret_steal(PYBIND11_BYTES_FROM_STRING_AND_SIZE(buffer, length)); @@ -1101,8 +1101,8 @@ inline bytes::bytes(const pybind11::str &s) { } inline str::str(const bytes& b) { - char *buffer; - ssize_t length; + char *buffer = nullptr; + ssize_t length = 0; if (PYBIND11_BYTES_AS_STRING_AND_SIZE(b.ptr(), &buffer, &length)) pybind11_fail("Unable to extract bytes contents!"); auto obj = reinterpret_steal(PyUnicode_FromStringAndSize(buffer, (ssize_t) length)); diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 0041d80f6..d6b75874f 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -217,9 +217,10 @@ void vector_modifiers(enable_if_t Vector * { - size_t start, stop, step, slicelength; + size_t start = 0, stop = 0, step = 0, slicelength = 0; if (!slice.compute(v.size(), &start, &stop, &step, &slicelength)) throw error_already_set(); @@ -234,12 +235,12 @@ void vector_modifiers(enable_if_t), diff --git a/tests/test_eval.cpp b/tests/test_eval.cpp index 66cdf7105..16cdf17f2 100644 --- a/tests/test_eval.cpp +++ b/tests/test_eval.cpp @@ -66,7 +66,7 @@ TEST_SUBMODULE(eval_, m) { auto local = py::dict(); local["y"] = py::int_(43); - int val_out; + int val_out = 0; local["call_test2"] = py::cpp_function([&](int value) { val_out = value; }); auto result = py::eval_file(std::move(filename), global, local); diff --git a/tests/test_numpy_vectorize.cpp b/tests/test_numpy_vectorize.cpp index 42fb2002e..ed08a42be 100644 --- a/tests/test_numpy_vectorize.cpp +++ b/tests/test_numpy_vectorize.cpp @@ -92,7 +92,7 @@ TEST_SUBMODULE(numpy_vectorize, m) { [](const py::array_t &arg1, const py::array_t &arg2, const py::array_t &arg3) { - py::ssize_t ndim; + py::ssize_t ndim = 0; std::vector shape; std::array buffers{ {arg1.request(), arg2.request(), arg3.request()}}; diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp index c39798229..d49fb1f45 100644 --- a/tests/test_sequences_and_iterators.cpp +++ b/tests/test_sequences_and_iterators.cpp @@ -84,7 +84,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { py::class_(m, "Sliceable") .def(py::init()) .def("__getitem__", [](const Sliceable &s, const py::slice &slice) { - py::ssize_t start, stop, step, slicelength; + py::ssize_t start = 0, stop = 0, step = 0, slicelength = 0; if (!slice.compute(s.size, &start, &stop, &step, &slicelength)) throw py::error_already_set(); int istart = static_cast(start); @@ -204,7 +204,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { /// Slicing protocol (optional) .def("__getitem__", [](const Sequence &s, const py::slice &slice) -> Sequence * { - size_t start, stop, step, slicelength; + size_t start = 0, stop = 0, step = 0, slicelength = 0; if (!slice.compute(s.size(), &start, &stop, &step, &slicelength)) throw py::error_already_set(); auto *seq = new Sequence(slicelength); @@ -216,7 +216,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { }) .def("__setitem__", [](Sequence &s, const py::slice &slice, const Sequence &value) { - size_t start, stop, step, slicelength; + size_t start = 0, stop = 0, step = 0, slicelength = 0; if (!slice.compute(s.size(), &start, &stop, &step, &slicelength)) throw py::error_already_set(); if (slicelength != value.size())