From c1e97392be42ff697c1cad01c180668f683b8d6d Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 11 Aug 2019 06:53:03 -0700 Subject: [PATCH] Fix warnings --- include/fmt/chrono.h | 2 +- include/fmt/compile.h | 2 +- include/fmt/format.h | 27 +++++++++++++++------------ include/fmt/safe-duration-cast.h | 4 ++-- test/format-test.cc | 2 +- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index e2a47019..57ce9ef3 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -520,7 +520,7 @@ struct chrono_formatter { std::chrono::duration d) : context(ctx), out(o), val(d.count()), negative(false) { if (d.count() < 0) { - val = -val; + val = 0 - val; negative = true; } diff --git a/include/fmt/compile.h b/include/fmt/compile.h index d80adb73..530497df 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -358,7 +358,7 @@ template class compiletime_prepared_parts_type_provider { using value_type = format_part; }; - using type = conditional_t(number_of_format_parts), + using type = conditional_t, empty>; }; diff --git a/include/fmt/format.h b/include/fmt/format.h index b40e5d69..cf292f45 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1741,10 +1741,13 @@ class arg_formatter_base { } void write(const char_type* value) { - if (!value) FMT_THROW(format_error("string pointer is null")); - auto length = std::char_traits::length(value); - basic_string_view sv(value, length); - specs_ ? writer_.write(sv, *specs_) : writer_.write(sv); + if (!value) { + FMT_THROW(format_error("string pointer is null")); + } else { + auto length = std::char_traits::length(value); + basic_string_view sv(value, length); + specs_ ? writer_.write(sv, *specs_) : writer_.write(sv); + } } public: @@ -1851,7 +1854,7 @@ FMT_CONSTEXPR int parse_nonnegative_int(const Char*& begin, const Char* end, } unsigned value = 0; // Convert to unsigned to prevent a warning. - unsigned max_int = (std::numeric_limits::max)(); + constexpr unsigned max_int = (std::numeric_limits::max)(); unsigned big = max_int / 10; do { // Check for overflow. @@ -2054,7 +2057,7 @@ FMT_CONSTEXPR void set_dynamic_spec(T& value, FormatArg arg, ErrorHandler eh) { struct auto_id {}; template -FMT_CONSTEXPR typename Context::format_arg get_arg(Context& ctx, unsigned id) { +FMT_CONSTEXPR typename Context::format_arg get_arg(Context& ctx, int id) { auto arg = ctx.arg(id); if (!arg) ctx.on_error("argument index out of range"); return arg; @@ -2092,7 +2095,7 @@ class specs_handler : public specs_setter { return internal::get_arg(context_, parse_context_.next_arg_id()); } - FMT_CONSTEXPR format_arg get_arg(unsigned arg_id) { + FMT_CONSTEXPR format_arg get_arg(int arg_id) { parse_context_.check_arg_id(arg_id); return internal::get_arg(context_, arg_id); } @@ -2418,7 +2421,7 @@ inline bool find(const char* first, const char* last, char value, template struct id_adapter { FMT_CONSTEXPR void operator()() { handler.on_arg_id(); } - FMT_CONSTEXPR void operator()(unsigned id) { handler.on_arg_id(id); } + FMT_CONSTEXPR void operator()(int id) { handler.on_arg_id(id); } FMT_CONSTEXPR void operator()(basic_string_view id) { handler.on_arg_id(id); } @@ -2511,7 +2514,7 @@ class format_string_checker { arg_id_ = context_.next_arg_id(); check_arg_id(); } - FMT_CONSTEXPR void on_arg_id(unsigned id) { + FMT_CONSTEXPR void on_arg_id(int id) { arg_id_ = id; context_.check_arg_id(id); check_arg_id(); @@ -2642,7 +2645,7 @@ class FMT_API system_error : public std::runtime_error { protected: int error_code_; - system_error() : std::runtime_error("") {} + system_error() : std::runtime_error(""), error_code_(0) {} public: /** @@ -3156,10 +3159,10 @@ struct format_handler : internal::error_handler { context.advance_to(out); } - void get_arg(unsigned id) { arg = internal::get_arg(context, id); } + void get_arg(int id) { arg = internal::get_arg(context, id); } void on_arg_id() { get_arg(parse_context.next_arg_id()); } - void on_arg_id(unsigned id) { + void on_arg_id(int id) { parse_context.check_arg_id(id); get_arg(id); } diff --git a/include/fmt/safe-duration-cast.h b/include/fmt/safe-duration-cast.h index d0719512..aa036182 100644 --- a/include/fmt/safe-duration-cast.h +++ b/include/fmt/safe-duration-cast.h @@ -72,7 +72,7 @@ FMT_CONSTEXPR To lossless_integral_conversion(const From from, int& ec) { // yes, From always fits in To. } else { // from may not fit in To, we have to do a dynamic check - if (from > T::max()) { + if (from > static_cast(T::max())) { ec = 1; return {}; } @@ -85,7 +85,7 @@ FMT_CONSTEXPR To lossless_integral_conversion(const From from, int& ec) { // yes, From always fits in To. } else { // from may not fit in To, we have to do a dynamic check - if (from > T::max()) { + if (from > static_cast(T::max())) { // outside range. ec = 1; return {}; diff --git a/test/format-test.cc b/test/format-test.cc index e4631b08..dfc265fc 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1461,7 +1461,7 @@ TEST(FormatterTest, PrecisionRounding) { // Trigger rounding error in Grisu by a carefully chosen number. auto n = 3788512123356.985352; char buffer[64]; - sprintf(buffer, "%f", n); + safe_sprintf(buffer, "%f", n); EXPECT_EQ(buffer, format("{:f}", n)); }