From 7f23e9f3a47cb6b6373467c18962c16dfdd82fe7 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 27 Dec 2022 15:14:10 -0500 Subject: [PATCH] chore: update clang-tidy to 15 (#4387) * chore: update clang-tidy to 15 * Add git * Add NOLINTNEXTLINE for assignment in if * Update CONTRIBUTING.md * Add NOLINTNEXTLINE where needed * Add one more NOLINTNEXTLINE * stl_bind: make more readable * Another missing NOLINTNEXTLINE * Match style elsewhere * Apply reviewer suggestion. Mark false positive --- .github/CONTRIBUTING.md | 4 ++-- .github/workflows/format.yml | 4 ++-- include/pybind11/detail/internals.h | 3 +++ include/pybind11/stl.h | 1 + include/pybind11/stl_bind.h | 14 +++++++++----- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 00b1fea4c..ad7974395 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -235,8 +235,8 @@ directory inside your pybind11 git clone. Files will be modified in place, so you can use git to monitor the changes. ```bash -docker run --rm -v $PWD:/mounted_pybind11 -it silkeh/clang:13 -apt-get update && apt-get install -y python3-dev python3-pytest +docker run --rm -v $PWD:/mounted_pybind11 -it silkeh/clang:15-bullseye +apt-get update && apt-get install -y git python3-dev python3-pytest cmake -S /mounted_pybind11/ -B build -DCMAKE_CXX_CLANG_TIDY="$(which clang-tidy);--use-color" -DDOWNLOAD_EIGEN=ON -DDOWNLOAD_CATCH=ON -DCMAKE_CXX_STANDARD=17 cmake --build build -j 2 ``` diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index b18474bc3..46489feb3 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -38,12 +38,12 @@ jobs: # in .github/CONTRIBUTING.md and update as needed. name: Clang-Tidy runs-on: ubuntu-latest - container: silkeh/clang:13 + container: silkeh/clang:15-bullseye steps: - uses: actions/checkout@v3 - name: Install requirements - run: apt-get update && apt-get install -y python3-dev python3-pytest + run: apt-get update && apt-get install -y git python3-dev python3-pytest - name: Configure run: > diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index ef1849fbe..05e36ad18 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -469,12 +469,14 @@ PYBIND11_NOINLINE internals &get_internals() { #if defined(WITH_THREAD) PyThreadState *tstate = PyThreadState_Get(); + // NOLINTNEXTLINE(bugprone-assignment-in-if-condition) if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->tstate)) { pybind11_fail("get_internals: could not successfully initialize the tstate TSS key!"); } PYBIND11_TLS_REPLACE_VALUE(internals_ptr->tstate, tstate); # if PYBIND11_INTERNALS_VERSION > 4 + // NOLINTNEXTLINE(bugprone-assignment-in-if-condition) if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->loader_life_support_tls_key)) { pybind11_fail("get_internals: could not successfully initialize the " "loader_life_support TSS key!"); @@ -514,6 +516,7 @@ struct local_internals { struct shared_loader_life_support_data { PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key) shared_loader_life_support_data() { + // NOLINTNEXTLINE(bugprone-assignment-in-if-condition) if (!PYBIND11_TLS_KEY_CREATE(loader_life_support_tls_key)) { pybind11_fail("local_internals: could not successfully initialize the " "loader_life_support TLS key!"); diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 2d144b598..84c517108 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -316,6 +316,7 @@ struct optional_caster { if (!std::is_lvalue_reference::value) { policy = return_value_policy_override::policy(policy); } + // NOLINTNEXTLINE(bugprone-unchecked-optional-access) return value_conv::cast(*std::forward(src), policy, parent); } diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 0c634597e..7cfd996dd 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -355,13 +355,17 @@ void vector_accessor(enable_if_t::value, Class_> &cl) using DiffType = typename Vector::difference_type; using ItType = typename Vector::iterator; cl.def("__getitem__", [](const Vector &v, DiffType i) -> T { - if (i < 0 && (i += v.size()) < 0) { + if (i < 0) { + i += v.size(); + if (i < 0) { + throw index_error(); + } + } + auto i_st = static_cast(i); + if (i_st >= v.size()) { throw index_error(); } - if ((SizeType) i >= v.size()) { - throw index_error(); - } - return v[(SizeType) i]; + return v[i_st]; }); cl.def(