Fix use-after-move in test_parse_function_compiles and refactor

My recent patch had introduced a conditional use-after-move bug into the
test_parse_function_compiles function.  This patch fixes that by
reworking the entire test case into a compile-time check.  In my
opinion, we're not loosing anything by not actually executing the code
(the result wasn't looked at anyway) and the code becomes much clearer
by omitting the argument-preparation fluff.
This commit is contained in:
Moritz Klammler 2022-09-28 19:51:32 +02:00
parent e064a5c371
commit 3f197c3cab

View File

@ -989,38 +989,18 @@ BOOST_AUTO_TEST_CASE(test_file_ends_without_lf)
BOOST_AUTO_TEST_CASE(test_parse_function_compiles) BOOST_AUTO_TEST_CASE(test_parse_function_compiles)
{ {
const auto c = [](std::string& s) -> const std::string& { return s; };
/*mutable*/ std::string example = testinput("example.toml");
// toml::parse("");
using result_type = decltype(toml::parse("string literal")); using result_type = decltype(toml::parse("string literal"));
(void) [](const char* that) -> result_type { return toml::parse(that); };
BOOST_TEST_MESSAGE("string_literal"); (void) [](char* that) -> result_type { return toml::parse(that); };
(void) [](const std::string& that) -> result_type { return toml::parse(that); };
// toml::parse(const char*); (void) [](std::string& that) -> result_type { return toml::parse(that); };
const result_type cstring = toml::parse(example.c_str()); (void) [](std::string&& that) -> result_type { return toml::parse(that); };
BOOST_TEST_MESSAGE("const char*");
// toml::parse(char*);
const result_type char_ptr = toml::parse(&example.front());
BOOST_TEST_MESSAGE("char*");
// toml::parse(const std::string&);
const result_type string = toml::parse(c(example));
// toml::parse(std::string&);
const result_type string_mutref = toml::parse(example);
// toml::parse(std::string&&);
const result_type string_rref = toml::parse(std::move(example));
BOOST_TEST_MESSAGE("strings");
#ifdef TOML11_HAS_STD_FILESYSTEM #ifdef TOML11_HAS_STD_FILESYSTEM
const std::filesystem::path fname_path(example.begin(), example.end()); (void) [](const std::filesystem::path& that) -> result_type { return toml::parse(that); };
const result_type filesystem_path = toml::parse(fname_path); (void) [](std::filesystem::path& that) -> result_type { return toml::parse(that); };
BOOST_TEST_MESSAGE("path"); (void) [](std::filesystem::path&& that) -> result_type { return toml::parse(that); };
#endif #endif
(void) [](std::FILE* that) -> result_type { return toml::parse(that, "mandatory.toml"); };
} }
BOOST_AUTO_TEST_CASE(test_parse_nonexistent_file) BOOST_AUTO_TEST_CASE(test_parse_nonexistent_file)