Remove unreachable code found by the coverage analysis

Remove code that no input can reach, and mark the lines that cannot be
covered with LCOV_EXCL_LINE:

- BJData writer: the dry_run parameter of the ND-array element writers was
  always true; replace them by range checks.
- BON8 writer: call throw_integer_out_of_range() directly instead of
  write_bon8_value() for values that always throw.
- binary_writer: integer_bits() is only called for integers of up to 64 bits.
- serializer, view serializer, lexer: replace guards that the callers make
  unreachable by assertions or by the simpler code (surrogate pairs under
  ensure_ascii, next_unget, '-' at the end of the buffer).
- json_view: drop unreachable branches in scalar(), set(), check_utf8_scan(),
  append_input_impl() and unescape().
- Mark exception-cleanup braces, 2 GiB limits, big-endian and 32-bit code, and
  fall-through case labels with LCOV_EXCL_LINE.

No behavior changes for any input that reaches these functions.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-01 22:32:41 +01:00
1 parent 04a5accfb9
commit d7b9472173
21 files changed
+234 -404

No files matched your search

+13 -13
View File
@@ -400,7 +400,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
#elif defined(__GNUC__) || defined(__GNUG__)
result["compiler"] = {{"family", "gcc"}, {"version", detail::concat<string_t>(
detail::to_string<string_t>(static_cast<std::size_t>(__GNUC__)), '.',
detail::to_string<string_t>(static_cast<std::size_t>(__GNUC__)), '.', // LCOV_EXCL_LINE
detail::to_string<string_t>(static_cast<std::size_t>(__GNUC_MINOR__)), '.',
detail::to_string<string_t>(static_cast<std::size_t>(__GNUC_PATCHLEVEL__)))
}
@@ -672,14 +672,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
case value_t::binary:
binary = create<binary_t>();
break;
case value_t::null:
case value_t::null: // LCOV_EXCL_LINE
case value_t::boolean:
case value_t::number_integer:
case value_t::number_unsigned:
case value_t::number_float:
case value_t::discarded:
default:
break;
break; // LCOV_EXCL_LINE
}
}
@@ -1034,14 +1034,14 @@ public:
destroy_container(t);
break;
case value_t::null:
case value_t::null: // LCOV_EXCL_LINE
case value_t::boolean:
case value_t::number_integer:
case value_t::number_unsigned:
case value_t::number_float:
case value_t::discarded:
default:
break;
break; // LCOV_EXCL_LINE
}
}
};
@@ -1214,9 +1214,9 @@ public:
return is_array() ? array_capacity() : object_capacity();
}
#else
static constexpr std::size_t container_capacity() noexcept
static constexpr std::size_t container_capacity() noexcept // LCOV_EXCL_LINE
{
return detail::unknown_size();
return detail::unknown_size(); // LCOV_EXCL_LINE
}
#endif
@@ -1402,7 +1402,7 @@ public:
/// into this switch is worth a few percent when copying a value made
/// mostly of numbers
JSON_HEDLEY_ALWAYS_INLINE
static void copy_leaf_value(const basic_json& src, basic_json& dst)
static void copy_leaf_value(const basic_json& src, basic_json& dst) // LCOV_EXCL_LINE
{
switch (src.m_data.m_type)
{
@@ -5554,7 +5554,7 @@ public:
/// their inlined code at each call site small. Binary values are
/// rare and left to equal_general().
JSON_HEDLEY_ALWAYS_INLINE
static int equal_leaf(const_reference lhs, const_reference rhs) noexcept
static int equal_leaf(const_reference lhs, const_reference rhs) noexcept // LCOV_EXCL_LINE
{
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_PUSH
@@ -6863,7 +6863,7 @@ public:
basic_json result(value_t::object);
json_pointer::flatten("", *this, result);
return result;
}
} // LCOV_EXCL_LINE (exception cleanup only)
/// @brief unflatten a previously flattened JSON value
/// @sa https://json.nlohmann.me/api/basic_json/unflatten/
@@ -7576,7 +7576,7 @@ public:
result.append(path.data(), path.size());
diff_append_key(result, key);
return result;
}
} // LCOV_EXCL_LINE (exception cleanup only)
/// @brief the path of the element @a index of the value at @a path
static string_t diff_index_path(const string_t& path, const std::size_t index)
@@ -7586,7 +7586,7 @@ public:
result.append(path.data(), path.size());
diff_append_index(result, index);
return result;
}
} // LCOV_EXCL_LINE (exception cleanup only)
/// @brief append the "remove" operations for the elements of array
/// @a source from @a index on, and the "add" operations for the
@@ -7746,7 +7746,7 @@ public:
// operations
if (source == target)
{
return false;
return false; // LCOV_EXCL_LINE (equal objects with the same members in the same order return early)
}
// slow path: the common keys are in a different relative