diff --git a/.codespell-ignorelines b/.codespell-ignorelines index eaea1f128..5781de01a 100644 --- a/.codespell-ignorelines +++ b/.codespell-ignorelines @@ -1,8 +1,8 @@ assert m.atyp_valu().get_mtxt() == "Valu" atyp_valu rtrn_valu() { atyp_valu obj{"Valu"}; return obj; } - indestructible_int(int v) : valu{v} {} + explicit indestructible_int(int v) : valu{v} {} + explicit movable_int(int v) : valu{v} {} int valu; - movable_int(int v) : valu{v} {} (m.pass_valu, "Valu", "pass_valu:Valu(_MvCtor)*_CpCtor"), other.valu = 91; REQUIRE(hld.as_raw_ptr_unowned()->valu == 19); diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index 161203598..98e322cdc 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -43,7 +43,7 @@ inline bool deregister_instance(instance *self, void *valptr, const type_info *t // clang-format off class modified_type_caster_generic_load_impl { public: - PYBIND11_NOINLINE modified_type_caster_generic_load_impl(const std::type_info &type_info) + PYBIND11_NOINLINE explicit modified_type_caster_generic_load_impl(const std::type_info &type_info) : typeinfo(get_type_info(type_info)), cpptype(&type_info) { } explicit modified_type_caster_generic_load_impl(const type_info *typeinfo = nullptr) @@ -598,13 +598,17 @@ struct smart_holder_type_caster : smart_holder_type_caster_load, // The const operators here prove that the existing type_caster mechanism already supports // const-correctness. However, fully implementing const-correctness inside this type_caster // is still a major project. + // NOLINTNEXTLINE(google-explicit-constructor) operator T const &() const { return const_cast(this)->loaded_as_lvalue_ref(); } + // NOLINTNEXTLINE(google-explicit-constructor) operator T const *() const { return const_cast(this)->loaded_as_raw_ptr_unowned(); } + // NOLINTNEXTLINE(google-explicit-constructor) operator T &() { return this->loaded_as_lvalue_ref(); } + // NOLINTNEXTLINE(google-explicit-constructor) operator T *() { return this->loaded_as_raw_ptr_unowned(); } // Originally type_caster_generic::cast. @@ -749,6 +753,7 @@ struct smart_holder_type_caster> : smart_holder_type_caster_l template using cast_op_type = std::shared_ptr; + // NOLINTNEXTLINE(google-explicit-constructor) operator std::shared_ptr() { return this->loaded_as_shared_ptr(); } }; @@ -768,6 +773,7 @@ struct smart_holder_type_caster> : smart_holder_type_ca template using cast_op_type = std::shared_ptr; + // NOLINTNEXTLINE(google-explicit-constructor) operator std::shared_ptr() { return this->loaded_as_shared_ptr(); } // Mutbl2Const }; @@ -844,6 +850,7 @@ struct smart_holder_type_caster> : smart_holder_type_caste template using cast_op_type = std::unique_ptr; + // NOLINTNEXTLINE(google-explicit-constructor) operator std::unique_ptr() { return this->template loaded_as_unique_ptr(); } }; @@ -863,6 +870,7 @@ struct smart_holder_type_caster> template using cast_op_type = std::unique_ptr; + // NOLINTNEXTLINE(google-explicit-constructor) operator std::unique_ptr() { return this->template loaded_as_unique_ptr(); } }; diff --git a/tests/pure_cpp/smart_holder_poc_test.cpp b/tests/pure_cpp/smart_holder_poc_test.cpp index fa57c38bf..e414930a8 100644 --- a/tests/pure_cpp/smart_holder_poc_test.cpp +++ b/tests/pure_cpp/smart_holder_poc_test.cpp @@ -9,7 +9,7 @@ namespace helpers { struct movable_int { int valu; - movable_int(int v) : valu{v} {} + explicit movable_int(int v) : valu{v} {} movable_int(movable_int &&other) noexcept { valu = other.valu; other.valu = 91; @@ -26,7 +26,7 @@ struct functor_other_delete : functor_builtin_delete {}; struct indestructible_int { int valu; - indestructible_int(int v) : valu{v} {} + explicit indestructible_int(int v) : valu{v} {} private: ~indestructible_int() = default; diff --git a/tests/test_class_sh_basic.cpp b/tests/test_class_sh_basic.cpp index 332f10a28..21e307a2d 100644 --- a/tests/test_class_sh_basic.cpp +++ b/tests/test_class_sh_basic.cpp @@ -12,7 +12,7 @@ namespace class_sh_basic { struct atyp { // Short for "any type". std::string mtxt; atyp() : mtxt("DefaultConstructor") {} - atyp(const std::string &mtxt_) : mtxt(mtxt_) {} + explicit atyp(const std::string &mtxt_) : mtxt(mtxt_) {} atyp(const atyp &other) { mtxt = other.mtxt + "_CpCtor"; } atyp(atyp &&other) noexcept { mtxt = other.mtxt + "_MvCtor"; } }; diff --git a/tests/test_class_sh_disowning.cpp b/tests/test_class_sh_disowning.cpp index b9dc1e60b..a84571c60 100644 --- a/tests/test_class_sh_disowning.cpp +++ b/tests/test_class_sh_disowning.cpp @@ -10,7 +10,7 @@ namespace class_sh_disowning { template // Using int as a trick to easily generate a series of types. struct Atype { int val = 0; - Atype(int val_) : val{val_} {} + explicit Atype(int val_) : val{val_} {} int get() const { return val * 10 + SerNo; } }; diff --git a/tests/test_class_sh_disowning_mi.cpp b/tests/test_class_sh_disowning_mi.cpp index 1ff6ef7bb..9c7ef6041 100644 --- a/tests/test_class_sh_disowning_mi.cpp +++ b/tests/test_class_sh_disowning_mi.cpp @@ -31,13 +31,13 @@ void disown_b(std::unique_ptr) {} // test_multiple_inheritance_python struct Base1 { - Base1(int i) : i(i) {} + explicit Base1(int i) : i(i) {} int foo() const { return i; } int i; }; struct Base2 { - Base2(int j) : j(j) {} + explicit Base2(int j) : j(j) {} int bar() const { return j; } int j; }; diff --git a/tests/test_class_sh_shared_ptr_copy_move.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp index c46c2c7a9..2828b6710 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -14,7 +14,7 @@ const std::string fooNames[] = {"ShPtr_", "SmHld_"}; template struct Foo { std::string history; - Foo(const std::string &history_) : history(history_) {} + explicit Foo(const std::string &history_) : history(history_) {} Foo(const Foo &other) : history(other.history + "_CpCtor") {} Foo(Foo &&other) noexcept : history(other.history + "_MvCtor") {} Foo &operator=(const Foo &other) { diff --git a/tests/test_class_sh_trampoline_basic.cpp b/tests/test_class_sh_trampoline_basic.cpp index e9f41b71a..a3de5344b 100644 --- a/tests/test_class_sh_trampoline_basic.cpp +++ b/tests/test_class_sh_trampoline_basic.cpp @@ -11,7 +11,7 @@ template // Using int as a trick to easily generate a series of type struct Abase { int val = 0; virtual ~Abase() = default; - Abase(int val_) : val{val_} {} + explicit Abase(int val_) : val{val_} {} int Get() const { return val * 10 + 3; } virtual int Add(int other_val) const = 0;