Satisfy clang-tidy in the new bulk scanner tests

- give the helper lambdas an explicit std::string return type and return
  braced initializer lists (modernize-return-braced-init-list)
- replace the C-style array of test cases with a std::vector
  (modernize-avoid-c-arrays)
- silence pro-type-member-init on the two brace-initialized aggregates;
  default member initializers would stop them being aggregates in C++11

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-08-20 04:09:58 +02:00
parent a5e94f4e8b
commit 5edfccadf0
2 changed files with 21 additions and 17 deletions
+13 -11
View File
@@ -313,7 +313,7 @@ TEST_CASE("lexer number fast path")
// full outcome of parsing @a doc, so a mismatch in type, value, or error
// message is caught, not just a mismatch in acceptance
const auto outcome = [](const std::string & doc, bool streaming)
const auto outcome = [](const std::string & doc, bool streaming) -> std::string
{
try
{
@@ -328,7 +328,7 @@ TEST_CASE("lexer number fast path")
}
catch (const json::parse_error& e)
{
return std::string(e.what());
return {e.what()};
}
};
@@ -370,7 +370,7 @@ TEST_CASE("lexer number fast path")
// by a newline is the interesting case, because the byte path reaches the
// newline (which resets the column) and then ungets it.
// returns the parse_error message, or "" if the document parsed
const auto contiguous_error = [](const std::string & doc)
const auto contiguous_error = [](const std::string & doc) -> std::string
{
try
{
@@ -379,11 +379,11 @@ TEST_CASE("lexer number fast path")
}
catch (const json::parse_error& e)
{
return std::string(e.what());
return {e.what()};
}
return std::string();
return {};
};
const auto streaming_error = [](const std::string & doc)
const auto streaming_error = [](const std::string & doc) -> std::string
{
try
{
@@ -393,9 +393,9 @@ TEST_CASE("lexer number fast path")
}
catch (const json::parse_error& e)
{
return std::string(e.what());
return {e.what()};
}
return std::string();
return {};
};
for (const char* bad :
@@ -439,7 +439,7 @@ TEST_CASE("lexer string fast path")
// the full outcome of parsing @a doc: the parsed value, or the exact error
// message, so a mismatch in either is caught. Only usable with exceptions
// on: parsing invalid input aborts when they are off.
const auto outcome = [](const std::string & doc, bool streaming)
const auto outcome = [](const std::string & doc, bool streaming) -> std::string
{
try
{
@@ -457,7 +457,7 @@ TEST_CASE("lexer string fast path")
// as a reported mismatch rather than as an uncaught exception
catch (const json::exception& e)
{
return std::string(e.what());
return {e.what()};
}
};
#endif
@@ -547,7 +547,9 @@ TEST_CASE("lexer string fast path")
{
// The bulk validator must accept exactly what the byte-at-a-time
// scanner accepts, so pin the boundaries of every range it recognizes.
struct utf8_case
// aggregate, only ever brace-initialized below; default member
// initializers would stop it being an aggregate in C++11
struct utf8_case // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
{
std::string sequence;
bool valid;
+8 -6
View File
@@ -19,6 +19,8 @@
using nlohmann::json;
#include <list>
#include <string> // string
#include <vector> // vector
#if defined(__cpp_lib_concepts) && defined(JSON_HAS_CPP_20)
#include <iterator>
@@ -333,7 +335,7 @@ TEST_CASE("std::counted_iterator bulk scanning stops at the counted end")
// input: the bulk scanners must never look at the bytes behind it, even
// though they are readable. Each case is compared against parsing the
// equivalent prefix as a std::string.
const auto via_counted = [](const std::string & buf, std::size_t n)
const auto via_counted = [](const std::string & buf, std::size_t n) -> std::string
{
const std::counted_iterator<const char*> first(buf.data(), static_cast<std::iter_difference_t<const char*>>(n));
try
@@ -343,10 +345,10 @@ TEST_CASE("std::counted_iterator bulk scanning stops at the counted end")
}
catch (const json::parse_error& e)
{
return std::string(e.what());
return {e.what()};
}
};
const auto via_prefix = [](const std::string & buf, std::size_t n)
const auto via_prefix = [](const std::string & buf, std::size_t n) -> std::string
{
try
{
@@ -355,16 +357,16 @@ TEST_CASE("std::counted_iterator bulk scanning stops at the counted end")
}
catch (const json::parse_error& e)
{
return std::string(e.what());
return {e.what()};
}
};
struct testcase
struct testcase // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
{
const char* buffer;
std::size_t count;
};
const testcase cases[] =
const std::vector<testcase> cases =
{
{"[\"abc\"]____TRAILING____", 7}, // exact fit, tail hidden
{"[\"abcdefghijklmnop\"]____", 8}, // cut inside a string