From 352bd25ad670c51259429c403024256830bfb7b5 Mon Sep 17 00:00:00 2001 From: Anthony Printup <92564080+anthonyprintup@users.noreply.github.com> Date: Fri, 8 May 2026 22:07:05 +0200 Subject: [PATCH] fix: use empty checks in runtime and codegen --- smoke/generated/example.protocyte.hpp | 18 +++++++++--------- smoke/generated/protocyte/runtime/runtime.hpp | 8 ++++---- smoke/src/host_smoke.cpp | 18 +++++++++--------- src/protocyte/cpp.py | 2 +- src/protocyte/runtime/runtime.hpp | 8 ++++---- tests/test_plugin.py | 13 +++++++------ 6 files changed, 34 insertions(+), 33 deletions(-) diff --git a/smoke/generated/example.protocyte.hpp b/smoke/generated/example.protocyte.hpp index 049ab8b..88d25a4 100644 --- a/smoke/generated/example.protocyte.hpp +++ b/smoke/generated/example.protocyte.hpp @@ -929,7 +929,7 @@ namespace test::ultimate { } } } - if (values_.size() != 0u && values_.size() != 3u) { + if (!values_.empty() && values_.size() != 3u) { return ::protocyte::unexpected(::protocyte::ErrorCode::invalid_argument, {}, static_cast<::protocyte::u32>(FieldNumber::values)); } @@ -937,7 +937,7 @@ namespace test::ultimate { } template::protocyte::Status serialize(Writer &writer) const noexcept { - if (values_.size() != 0u && values_.size() != 3u) { + if (!values_.empty() && values_.size() != 3u) { return ::protocyte::unexpected(::protocyte::ErrorCode::invalid_argument, {}, static_cast<::protocyte::u32>(FieldNumber::values)); } @@ -952,7 +952,7 @@ namespace test::ultimate { } ::protocyte::Result<::protocyte::usize> encoded_size() const noexcept { - if (values_.size() != 0u && values_.size() != 3u) { + if (!values_.empty() && values_.size() != 3u) { return ::protocyte::unexpected(::protocyte::ErrorCode::invalid_argument, {}, static_cast<::protocyte::u32>(FieldNumber::values)); } @@ -4254,11 +4254,11 @@ namespace test::ultimate { } } } - if (fixed_integer_array_.size() != 0u && fixed_integer_array_.size() != 3u) { + if (!fixed_integer_array_.empty() && fixed_integer_array_.size() != 3u) { return ::protocyte::unexpected(::protocyte::ErrorCode::invalid_argument, {}, static_cast<::protocyte::u32>(FieldNumber::fixed_integer_array)); } - if (fixed_repeated_byte_array_.size() != 0u && fixed_repeated_byte_array_.size() != 3u) { + if (!fixed_repeated_byte_array_.empty() && fixed_repeated_byte_array_.size() != 3u) { return ::protocyte::unexpected(::protocyte::ErrorCode::invalid_argument, {}, static_cast<::protocyte::u32>(FieldNumber::fixed_repeated_byte_array)); } @@ -4266,11 +4266,11 @@ namespace test::ultimate { } template::protocyte::Status serialize(Writer &writer) const noexcept { - if (fixed_integer_array_.size() != 0u && fixed_integer_array_.size() != 3u) { + if (!fixed_integer_array_.empty() && fixed_integer_array_.size() != 3u) { return ::protocyte::unexpected(::protocyte::ErrorCode::invalid_argument, {}, static_cast<::protocyte::u32>(FieldNumber::fixed_integer_array)); } - if (fixed_repeated_byte_array_.size() != 0u && fixed_repeated_byte_array_.size() != 3u) { + if (!fixed_repeated_byte_array_.empty() && fixed_repeated_byte_array_.size() != 3u) { return ::protocyte::unexpected(::protocyte::ErrorCode::invalid_argument, {}, static_cast<::protocyte::u32>(FieldNumber::fixed_repeated_byte_array)); } @@ -4933,11 +4933,11 @@ namespace test::ultimate { } ::protocyte::Result<::protocyte::usize> encoded_size() const noexcept { - if (fixed_integer_array_.size() != 0u && fixed_integer_array_.size() != 3u) { + if (!fixed_integer_array_.empty() && fixed_integer_array_.size() != 3u) { return ::protocyte::unexpected(::protocyte::ErrorCode::invalid_argument, {}, static_cast<::protocyte::u32>(FieldNumber::fixed_integer_array)); } - if (fixed_repeated_byte_array_.size() != 0u && fixed_repeated_byte_array_.size() != 3u) { + if (!fixed_repeated_byte_array_.empty() && fixed_repeated_byte_array_.size() != 3u) { return ::protocyte::unexpected(::protocyte::ErrorCode::invalid_argument, {}, static_cast<::protocyte::u32>(FieldNumber::fixed_repeated_byte_array)); } diff --git a/smoke/generated/protocyte/runtime/runtime.hpp b/smoke/generated/protocyte/runtime/runtime.hpp index 0923ec6..de7b3af 100644 --- a/smoke/generated/protocyte/runtime/runtime.hpp +++ b/smoke/generated/protocyte/runtime/runtime.hpp @@ -1390,7 +1390,7 @@ namespace protocyte { template constexpr Result> checked_span_of(const Span view) noexcept { - if (view.size() != 0u && view.data() == nullptr) { + if (!view.empty() && view.data() == nullptr) { return protocyte::unexpected(ErrorCode::invalid_argument, {}); } return view; @@ -1462,7 +1462,7 @@ namespace protocyte { if (!size) { return protocyte::unexpected(size.error()); } - if (*size != 0u && view.data() == nullptr) { + if (!view.empty() && view.data() == nullptr) { return protocyte::unexpected(ErrorCode::invalid_argument, {}); } return Span {reinterpret_cast(view.data()), *size}; @@ -1570,7 +1570,7 @@ namespace protocyte { if (lhs.size() != rhs.size()) { return false; } - if (!lhs.size()) { + if (lhs.empty()) { return true; } if (::std::is_constant_evaluated()) { @@ -3303,7 +3303,7 @@ namespace protocyte { if (*next_size > max_size()) { return protocyte::unexpected(ErrorCode::count_limit, {}); } - if (!buckets_.size() || *next_size >= rehash_threshold_for(buckets_.size())) { + if (buckets_.empty() || *next_size >= rehash_threshold_for(buckets_.size())) { return reserve(*next_size); } return {}; diff --git a/smoke/src/host_smoke.cpp b/smoke/src/host_smoke.cpp index 5545f6a..92ebbc2 100644 --- a/smoke/src/host_smoke.cpp +++ b/smoke/src/host_smoke.cpp @@ -972,7 +972,7 @@ namespace { REQUIRE(moved.has_oneof_bytes()); CHECK(view_equal(moved.oneof_bytes(), view_of(oneof_bytes))); CHECK(source.special_oneof_case() == Message::Special_oneofCase::none); - CHECK(source.oneof_bytes().size() == 0u); + CHECK(source.oneof_bytes().empty()); } SECTION("move assignment transfers active message case") { @@ -1176,7 +1176,7 @@ namespace { SECTION("unset fixed bytes stay absent through round trip") { Message message(ctx); CHECK_FALSE(message.has_sha256()); - CHECK(message.sha256().size() == 0u); + CHECK(message.sha256().empty()); populate_required_fixed_array(message, ctx); auto encoded_size = message.encoded_size(); @@ -1191,7 +1191,7 @@ namespace { protocyte::SliceReader reader(encoded, writer.position()); require_success(parsed.merge_from(reader)); CHECK_FALSE(parsed.has_sha256()); - CHECK(parsed.sha256().size() == 0u); + CHECK(parsed.sha256().empty()); } SECTION("mutable fixed bytes mark presence and zero initialize") { @@ -1218,7 +1218,7 @@ namespace { message.clear_sha256(); CHECK_FALSE(message.has_sha256()); - CHECK(message.sha256().size() == 0u); + CHECK(message.sha256().empty()); } SECTION("explicit zero bytes stay present through round trip") { @@ -1376,8 +1376,8 @@ namespace { CHECK(target_fixed_integer_array[i] == fixed_integer_array_values[i]); } CHECK(source.byte_array_size() == 0u); - CHECK(source.integer_array().size() == 0u); - CHECK(source.fixed_integer_array().size() == 0u); + CHECK(source.integer_array().empty()); + CHECK(source.fixed_integer_array().empty()); } SECTION("partially populated fixed arrays are rejected before encoding") { @@ -2528,8 +2528,8 @@ TEST_CASE("Protocyte encoding matches protobuf runtime bytes", "[smoke][compat]" auto parsed = parse_compat_bytes(ctx, compat_cases::unknown_fields); CHECK(parsed.f_int32() == 321); - CHECK(parsed.map_str_int32().size() == 0u); - CHECK(parsed.map_int32_str().size() == 0u); + CHECK(parsed.map_str_int32().empty()); + CHECK(parsed.map_int32_str().empty()); } } @@ -3462,7 +3462,7 @@ TEST_CASE("runtime limits are enforced for mutation and parsing", "[smoke][runti Message message(ctx); const auto view = message.mutable_sha256(); CHECK(view.data() == nullptr); - CHECK(view.size() == 0u); + CHECK(view.empty()); } SECTION("nested message fields respect max_message_bytes") { diff --git a/src/protocyte/cpp.py b/src/protocyte/cpp.py index baee7cd..6f75ee7 100644 --- a/src/protocyte/cpp.py +++ b/src/protocyte/cpp.py @@ -1050,7 +1050,7 @@ def _emit_fixed_array_validation(w: CppWriter, message: MessageModel, *, for_siz continue if item.repeated: condition = ( - f"{_member(item)}.size() != 0u && " + f"!{_member(item)}.empty() && " f"{_member(item)}.size() != {_array_max_literal(item)}" ) else: diff --git a/src/protocyte/runtime/runtime.hpp b/src/protocyte/runtime/runtime.hpp index 0923ec6..de7b3af 100644 --- a/src/protocyte/runtime/runtime.hpp +++ b/src/protocyte/runtime/runtime.hpp @@ -1390,7 +1390,7 @@ namespace protocyte { template constexpr Result> checked_span_of(const Span view) noexcept { - if (view.size() != 0u && view.data() == nullptr) { + if (!view.empty() && view.data() == nullptr) { return protocyte::unexpected(ErrorCode::invalid_argument, {}); } return view; @@ -1462,7 +1462,7 @@ namespace protocyte { if (!size) { return protocyte::unexpected(size.error()); } - if (*size != 0u && view.data() == nullptr) { + if (!view.empty() && view.data() == nullptr) { return protocyte::unexpected(ErrorCode::invalid_argument, {}); } return Span {reinterpret_cast(view.data()), *size}; @@ -1570,7 +1570,7 @@ namespace protocyte { if (lhs.size() != rhs.size()) { return false; } - if (!lhs.size()) { + if (lhs.empty()) { return true; } if (::std::is_constant_evaluated()) { @@ -3303,7 +3303,7 @@ namespace protocyte { if (*next_size > max_size()) { return protocyte::unexpected(ErrorCode::count_limit, {}); } - if (!buckets_.size() || *next_size >= rehash_threshold_for(buckets_.size())) { + if (buckets_.empty() || *next_size >= rehash_threshold_for(buckets_.size())) { return reserve(*next_size); } return {}; diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 3cc56c8..4f7b8b8 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -84,7 +84,7 @@ def test_runtime_container_growth_checks_capacity_limits() -> None: in runtime_header ) assert "const usize target {count * 2u};" in runtime_header - assert "next_size >= rehash_threshold_for(buckets_.size())" in runtime_header + assert "buckets_.empty() || *next_size >= rehash_threshold_for(buckets_.size())" in runtime_header assert "static constexpr usize rehash_threshold_for(const usize bucket_count) noexcept" in runtime_header assert "capacity_ * 2u" not in runtime_header assert "checked_mul(count, 2u, &target)" not in runtime_header @@ -130,7 +130,8 @@ def test_runtime_sequence_containers_accept_spans() -> None: assert "contiguous_range_size" not in runtime_header assert "const auto first_addr = reinterpret_cast(first);" in runtime_header assert "const auto last_addr = reinterpret_cast(last);" in runtime_header - assert "if (view.size() != 0u && view.data() == nullptr)" in runtime_header + assert "if (!view.empty() && view.data() == nullptr)" in runtime_header + assert "if (view.size() != 0u && view.data() == nullptr)" not in runtime_header assert "if (*size != 0u && value.data() == nullptr)" in runtime_header assert "if (last < first)" not in runtime_header assert "template Status assign(const Range &values) noexcept" in vector_body @@ -193,7 +194,7 @@ def test_runtime_byte_containers_use_bulk_copy_helpers() -> None: assert "::std::memmove(dst, src, count);" in runtime_header assert "::std::memcpy(dst, src, count);" in runtime_header assert "constexpr bool bytes_equal(const Span lhs, const Span rhs) noexcept" in runtime_header - assert "if (!lhs.size())" in runtime_header + assert "if (lhs.empty())" in runtime_header assert "if (::std::is_constant_evaluated())" in runtime_header assert "return ::std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;" in runtime_header assert "template using ByteArray = Array;" in runtime_header @@ -771,8 +772,8 @@ def test_checked_smoke_output_reflects_copy_propagation() -> None: assert "if (const auto st = out->copy_from(*this); !st) {" in header assert "return has_recursive_self() ? recursive_self_.operator->() : nullptr;" in header assert "static_cast<::protocyte::u32>(FieldNumber::recursive_self), *recursive_self_" in header - assert "fixed_integer_array_.size() != 0u && fixed_integer_array_.size() != 3u" in header - assert "fixed_repeated_byte_array_.size() != 0u && fixed_repeated_byte_array_.size() != 3u" in header + assert "!fixed_integer_array_.empty() && fixed_integer_array_.size() != 3u" in header + assert "!fixed_repeated_byte_array_.empty() && fixed_repeated_byte_array_.size() != 3u" in header assert ( "for (const auto &packed_value_remote_values : remote_values_) {\n" " {\n" @@ -1108,7 +1109,7 @@ def test_generated_header_emits_constants_and_array_storage() -> None: assert "return view.status();" in header assert "if (const auto st = blob_.assign(*view); !st)" in header assert "if (*len != 32u)" in header - assert "if (values_.size() != 0u && values_.size() != 4u) {" in header + assert "if (!values_.empty() && values_.size() != 4u) {" in header assert "template struct Array" in runtime_header assert "template using ByteArray = Array;" in runtime_header assert "template struct FixedByteArray" in runtime_header