From 21c8b5c1421ae04df00a97e4c396ffb7b26916bc Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 30 Jul 2020 07:16:15 -0700 Subject: [PATCH] Report error on missing named argument (#1796) --- include/fmt/core.h | 2 +- test/format-test.cc | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 4f662964..a9d07de5 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1913,7 +1913,7 @@ template class basic_format_args { } template int get_id(basic_string_view name) const { - if (!has_named_args()) return {}; + if (!has_named_args()) return -1; const auto& named_args = (is_packed() ? values_[-1] : args_[-1].value_).named_args; for (size_t i = 0; i < named_args.size; ++i) { diff --git a/test/format-test.cc b/test/format-test.cc index 31f8595f..6e6b6af9 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -543,7 +543,6 @@ TEST(FormatterTest, ManyArgs) { TEST(FormatterTest, NamedArg) { EXPECT_EQ("1/a/A", format("{_1}/{a_}/{A_}", fmt::arg("a_", 'a'), fmt::arg("A_", "A"), fmt::arg("_1", 1))); - EXPECT_THROW_MSG(format("{a}"), format_error, "argument not found"); EXPECT_EQ(" -42", format("{0:{width}}", -42, fmt::arg("width", 4))); EXPECT_EQ("st", format("{0:.{precision}}", "str", fmt::arg("precision", 2))); EXPECT_EQ("1 2", format("{} {two}", 1, fmt::arg("two", 2))); @@ -553,6 +552,8 @@ TEST(FormatterTest, NamedArg) { fmt::arg("i", 0), fmt::arg("j", 0), fmt::arg("k", 0), fmt::arg("l", 0), fmt::arg("m", 0), fmt::arg("n", 0), fmt::arg("o", 0), fmt::arg("p", 0))); + EXPECT_THROW_MSG(format("{a}"), format_error, "argument not found"); + EXPECT_THROW_MSG(format("{a}", 42), format_error, "argument not found"); } TEST(FormatterTest, AutoArgIndex) {