Fix the clang-tidy findings in the tests and headers of the API stack
- make_move_iterator_if_mutable takes the iterator by const reference
(performance-unnecessary-value-param).
- throw_patch_missing_member builds its message without a local that
is unused with JSON_NOEXCEPTION (bugprone-unused-local-non-trivial-variable).
- Tests: NOLINT the copies that test the copy constructor and the
constant mt19937 seed, NOLINT the deliberate "\xC3" "a" literal
concatenation, use references for the const views in
unit-throw_messages and return {} from its lambda.
A full clang-tidy 22 run over all 107 test translation units is clean.
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
7 files changed
+23
-24
No files matched your search
@@ -52,13 +52,13 @@ namespace detail
|
|||||||
// const (e.g., std::set): libstdc++ before GCC 5 defines the reference of a
|
// const (e.g., std::set): libstdc++ before GCC 5 defines the reference of a
|
||||||
// std::move_iterator as value_type&&, which cannot bind to a const element
|
// std::move_iterator as value_type&&, which cannot bind to a const element
|
||||||
template<typename Iterator>
|
template<typename Iterator>
|
||||||
std::move_iterator<Iterator> make_move_iterator_if_mutable_impl(Iterator it, std::true_type /*mutable*/)
|
std::move_iterator<Iterator> make_move_iterator_if_mutable_impl(const Iterator& it, std::true_type /*mutable*/)
|
||||||
{
|
{
|
||||||
return std::make_move_iterator(it);
|
return std::make_move_iterator(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Iterator>
|
template<typename Iterator>
|
||||||
Iterator make_move_iterator_if_mutable_impl(Iterator it, std::false_type /*mutable*/)
|
Iterator make_move_iterator_if_mutable_impl(const Iterator& it, std::false_type /*mutable*/)
|
||||||
{
|
{
|
||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
@@ -68,7 +68,7 @@ using is_mutable_iterator = std::integral_constant < bool,
|
|||||||
!std::is_const<typename std::remove_reference<decltype(*std::declval<Iterator&>())>::type>::value >;
|
!std::is_const<typename std::remove_reference<decltype(*std::declval<Iterator&>())>::type>::value >;
|
||||||
|
|
||||||
template<typename Iterator>
|
template<typename Iterator>
|
||||||
auto make_move_iterator_if_mutable(Iterator it)
|
auto make_move_iterator_if_mutable(const Iterator& it)
|
||||||
-> decltype(make_move_iterator_if_mutable_impl(it, is_mutable_iterator<Iterator> {}))
|
-> decltype(make_move_iterator_if_mutable_impl(it, is_mutable_iterator<Iterator> {}))
|
||||||
{
|
{
|
||||||
return make_move_iterator_if_mutable_impl(it, is_mutable_iterator<Iterator> {});
|
return make_move_iterator_if_mutable_impl(it, is_mutable_iterator<Iterator> {});
|
||||||
|
|||||||
@@ -494,8 +494,7 @@ JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE void throw_patch_missing_member(c
|
|||||||
static_cast<void>(string_type);
|
static_cast<void>(string_type);
|
||||||
static_cast<void>(val);
|
static_cast<void>(val);
|
||||||
// context-sensitive error message
|
// context-sensitive error message
|
||||||
const std::string error_msg = (op == "op") ? std::string("operation") : concat("operation '", op, '\'');
|
JSON_THROW(parse_error::create(exception_id::patch_invalid_operation, 0, concat((op == "op") ? std::string("operation") : concat("operation '", op, '\''), string_type ? " must have string member '" : " must have member '", member, "'"), &val));
|
||||||
JSON_THROW(parse_error::create(exception_id::patch_invalid_operation, 0, concat(error_msg, string_type ? " must have string member '" : " must have member '", member, "'"), &val));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename StringType, typename BasicJsonType>
|
template<typename StringType, typename BasicJsonType>
|
||||||
|
|||||||
Generated
+4
-5
@@ -5795,8 +5795,7 @@ JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE void throw_patch_missing_member(c
|
|||||||
static_cast<void>(string_type);
|
static_cast<void>(string_type);
|
||||||
static_cast<void>(val);
|
static_cast<void>(val);
|
||||||
// context-sensitive error message
|
// context-sensitive error message
|
||||||
const std::string error_msg = (op == "op") ? std::string("operation") : concat("operation '", op, '\'');
|
JSON_THROW(parse_error::create(exception_id::patch_invalid_operation, 0, concat((op == "op") ? std::string("operation") : concat("operation '", op, '\''), string_type ? " must have string member '" : " must have member '", member, "'"), &val));
|
||||||
JSON_THROW(parse_error::create(exception_id::patch_invalid_operation, 0, concat(error_msg, string_type ? " must have string member '" : " must have member '", member, "'"), &val));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename StringType, typename BasicJsonType>
|
template<typename StringType, typename BasicJsonType>
|
||||||
@@ -7209,13 +7208,13 @@ namespace detail
|
|||||||
// const (e.g., std::set): libstdc++ before GCC 5 defines the reference of a
|
// const (e.g., std::set): libstdc++ before GCC 5 defines the reference of a
|
||||||
// std::move_iterator as value_type&&, which cannot bind to a const element
|
// std::move_iterator as value_type&&, which cannot bind to a const element
|
||||||
template<typename Iterator>
|
template<typename Iterator>
|
||||||
std::move_iterator<Iterator> make_move_iterator_if_mutable_impl(Iterator it, std::true_type /*mutable*/)
|
std::move_iterator<Iterator> make_move_iterator_if_mutable_impl(const Iterator& it, std::true_type /*mutable*/)
|
||||||
{
|
{
|
||||||
return std::make_move_iterator(it);
|
return std::make_move_iterator(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Iterator>
|
template<typename Iterator>
|
||||||
Iterator make_move_iterator_if_mutable_impl(Iterator it, std::false_type /*mutable*/)
|
Iterator make_move_iterator_if_mutable_impl(const Iterator& it, std::false_type /*mutable*/)
|
||||||
{
|
{
|
||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
@@ -7225,7 +7224,7 @@ using is_mutable_iterator = std::integral_constant < bool,
|
|||||||
!std::is_const<typename std::remove_reference<decltype(*std::declval<Iterator&>())>::type>::value >;
|
!std::is_const<typename std::remove_reference<decltype(*std::declval<Iterator&>())>::type>::value >;
|
||||||
|
|
||||||
template<typename Iterator>
|
template<typename Iterator>
|
||||||
auto make_move_iterator_if_mutable(Iterator it)
|
auto make_move_iterator_if_mutable(const Iterator& it)
|
||||||
-> decltype(make_move_iterator_if_mutable_impl(it, is_mutable_iterator<Iterator> {}))
|
-> decltype(make_move_iterator_if_mutable_impl(it, is_mutable_iterator<Iterator> {}))
|
||||||
{
|
{
|
||||||
return make_move_iterator_if_mutable_impl(it, is_mutable_iterator<Iterator> {});
|
return make_move_iterator_if_mutable_impl(it, is_mutable_iterator<Iterator> {});
|
||||||
|
|||||||
@@ -198,11 +198,11 @@ TEST_CASE("copy constructor and destructor across value types")
|
|||||||
const json src_uint = 7u;
|
const json src_uint = 7u;
|
||||||
const json src_float = 2.5;
|
const json src_float = 2.5;
|
||||||
|
|
||||||
json c_null(src_null);
|
json c_null(src_null); // NOLINT(performance-unnecessary-copy-initialization): tests the copy constructor
|
||||||
json c_bool(src_bool);
|
json c_bool(src_bool); // NOLINT(performance-unnecessary-copy-initialization): tests the copy constructor
|
||||||
json c_int(src_int);
|
json c_int(src_int); // NOLINT(performance-unnecessary-copy-initialization): tests the copy constructor
|
||||||
json c_uint(src_uint);
|
json c_uint(src_uint); // NOLINT(performance-unnecessary-copy-initialization): tests the copy constructor
|
||||||
json c_float(src_float);
|
json c_float(src_float); // NOLINT(performance-unnecessary-copy-initialization): tests the copy constructor
|
||||||
|
|
||||||
CHECK(c_null.is_null());
|
CHECK(c_null.is_null());
|
||||||
CHECK(c_bool == true);
|
CHECK(c_bool == true);
|
||||||
|
|||||||
@@ -1230,7 +1230,7 @@ TEST_CASE("JSON pointer array index tokens match strtoull semantics")
|
|||||||
|
|
||||||
SECTION("random tokens")
|
SECTION("random tokens")
|
||||||
{
|
{
|
||||||
std::mt19937 gen(42);
|
std::mt19937 gen(42); // NOLINT(cert-msc32-c,cert-msc51-cpp,bugprone-random-generator-seed): reproducible
|
||||||
const std::string alphabet = "0123456789012345678901234567890123456789-+ ex";
|
const std::string alphabet = "0123456789012345678901234567890123456789-+ ex";
|
||||||
for (int i = 0; i < 5000; ++i)
|
for (int i = 0; i < 5000; ++i)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1435,7 +1435,7 @@ TEST_CASE("invalid UTF-8 at every position is handled like the reference")
|
|||||||
"\xF4\x90\x80\x80",
|
"\xF4\x90\x80\x80",
|
||||||
// sequences cut short by ASCII, a control character, a quote, a
|
// sequences cut short by ASCII, a control character, a quote, a
|
||||||
// backslash, DEL, or the lead byte of a well-formed character
|
// backslash, DEL, or the lead byte of a well-formed character
|
||||||
"\xC3" "a", "\xE2\x82" "\n", "\xE2\x82\"", "\xF0\x9F\x98\\", "\xC3\x7F", "\xC3\xC3\xA4", "\xE2\xF0\x9F\x98\x80",
|
"\xC3" "a", "\xE2\x82" "\n", "\xE2\x82\"", "\xF0\x9F\x98\\", "\xC3\x7F", "\xC3\xC3\xA4", "\xE2\xF0\x9F\x98\x80", // NOLINT(bugprone-suspicious-missing-comma): "\xC3" "a" ends the hex escape
|
||||||
// truncated sequences
|
// truncated sequences
|
||||||
"\xC3", "\xE2\x82", "\xF0\x9F\x98"
|
"\xC3", "\xE2\x82", "\xF0\x9F\x98"
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -54,8 +54,8 @@ TEST_CASE("exception messages of out-of-line throw sites")
|
|||||||
|
|
||||||
SECTION("at")
|
SECTION("at")
|
||||||
{
|
{
|
||||||
const json carr = arr;
|
const json& carr = arr;
|
||||||
const json cobj = obj;
|
const json& cobj = obj;
|
||||||
CHECK_MSG((void)arr.at(static_cast<json::size_type>(10)), "[json.exception.out_of_range.401] array index 10 is out of range");
|
CHECK_MSG((void)arr.at(static_cast<json::size_type>(10)), "[json.exception.out_of_range.401] array index 10 is out of range");
|
||||||
CHECK_MSG((void)carr.at(static_cast<json::size_type>(3)), "[json.exception.out_of_range.401] array index 3 is out of range");
|
CHECK_MSG((void)carr.at(static_cast<json::size_type>(3)), "[json.exception.out_of_range.401] array index 3 is out of range");
|
||||||
CHECK_MSG((void)num.at(static_cast<json::size_type>(0)), "[json.exception.type_error.304] cannot use at() with number");
|
CHECK_MSG((void)num.at(static_cast<json::size_type>(0)), "[json.exception.type_error.304] cannot use at() with number");
|
||||||
@@ -65,7 +65,7 @@ TEST_CASE("exception messages of out-of-line throw sites")
|
|||||||
CHECK_MSG((void)num.at("a"), "[json.exception.type_error.304] cannot use at() with number");
|
CHECK_MSG((void)num.at("a"), "[json.exception.type_error.304] cannot use at() with number");
|
||||||
|
|
||||||
nlohmann::ordered_json oobj = {{"a", 1}};
|
nlohmann::ordered_json oobj = {{"a", 1}};
|
||||||
const nlohmann::ordered_json coobj = oobj;
|
const nlohmann::ordered_json& coobj = oobj;
|
||||||
CHECK_MSG((void)oobj.at("z"), "[json.exception.out_of_range.403] key 'z' not found");
|
CHECK_MSG((void)oobj.at("z"), "[json.exception.out_of_range.403] key 'z' not found");
|
||||||
CHECK_MSG((void)coobj.at("z"), "[json.exception.out_of_range.403] key 'z' not found");
|
CHECK_MSG((void)coobj.at("z"), "[json.exception.out_of_range.403] key 'z' not found");
|
||||||
CHECK_MSG((void)coobj.at(std::string("zz")), "[json.exception.out_of_range.403] key 'zz' not found");
|
CHECK_MSG((void)coobj.at(std::string("zz")), "[json.exception.out_of_range.403] key 'zz' not found");
|
||||||
@@ -97,7 +97,7 @@ TEST_CASE("exception messages of out-of-line throw sites")
|
|||||||
#if !defined(JSON_NOEXCEPTION) // patch_msg() catches the exceptions, which JSON_NOEXCEPTION turns into std::abort()
|
#if !defined(JSON_NOEXCEPTION) // patch_msg() catches the exceptions, which JSON_NOEXCEPTION turns into std::abort()
|
||||||
SECTION("patch")
|
SECTION("patch")
|
||||||
{
|
{
|
||||||
const auto patch_msg = [](const char* doc, const char* patch)
|
const auto patch_msg = [](const char* doc, const char* patch) -> std::string
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -110,8 +110,9 @@ TEST_CASE("exception messages of out-of-line throw sites")
|
|||||||
// and with JSON_DIAGNOSTIC_POSITIONS its position like "(bytes 0-2) "; drop both
|
// and with JSON_DIAGNOSTIC_POSITIONS its position like "(bytes 0-2) "; drop both
|
||||||
std::string msg = e.what();
|
std::string msg = e.what();
|
||||||
for (const char* marker :
|
for (const char* marker :
|
||||||
{" (/", " (bytes "
|
{
|
||||||
})
|
" (/", " (bytes "
|
||||||
|
})
|
||||||
{
|
{
|
||||||
const auto pos = msg.find(marker);
|
const auto pos = msg.find(marker);
|
||||||
if (pos != std::string::npos)
|
if (pos != std::string::npos)
|
||||||
@@ -121,7 +122,7 @@ TEST_CASE("exception messages of out-of-line throw sites")
|
|||||||
}
|
}
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
return std::string();
|
return {};
|
||||||
};
|
};
|
||||||
|
|
||||||
CHECK(patch_msg("{}", "{}") == "[json.exception.parse_error.104] parse error: JSON patch must be an array of objects");
|
CHECK(patch_msg("{}", "{}") == "[json.exception.parse_error.104] parse error: JSON patch must be an array of objects");
|
||||||
|
|||||||
Reference in new issue
Block a user