From b54093c658ae5656799756ec5d4467f49064929f Mon Sep 17 00:00:00 2001 From: torsten48 <59708557+torsten48@users.noreply.github.com> Date: Fri, 3 Jul 2026 19:39:09 +0200 Subject: [PATCH 1/2] avoid MSC warning C4702: unreachable code (#4822) if FMT_CONSTEXPR is resolved to constexpr MSC warns about dead code after 'return' instruction. Explicitly using 'else' in these cases avoids the warning. --- include/fmt/base.h | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 60d1b792..aca28a8a 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -1285,9 +1285,12 @@ constexpr auto to_ascii(Char c) -> char { // Returns the number of code units in a code point or 1 on error. template FMT_CONSTEXPR auto code_point_length(const Char* begin) -> int { - if FMT_CONSTEXPR20 (sizeof(Char) != 1) return 1; - auto c = static_cast(*begin); - return static_cast((0x3a55000000000000ull >> (2 * (c >> 3))) & 3) + 1; + if FMT_CONSTEXPR20 (sizeof(Char) != 1) { + return 1; + } else { + auto c = static_cast(*begin); + return static_cast((0x3a55000000000000ull >> (2 * (c >> 3))) & 3) + 1; + } } // Parses the range [begin, end) as an unsigned integer. This function assumes @@ -2896,10 +2899,12 @@ FMT_API void vprint_buffered(FILE* f, string_view fmt, format_args args); template FMT_INLINE void print(format_string fmt, T&&... args) { vargs va = {{args...}}; - if FMT_CONSTEXPR20 (!detail::use_utf8) - return detail::vprint_mojibake(stdout, fmt.str, va, false); - detail::is_locking() ? vprint_buffered(stdout, fmt.str, va) - : vprint(fmt.str, va); + if FMT_CONSTEXPR20 (!detail::use_utf8) { + detail::vprint_mojibake(stdout, fmt.str, va, false); + } else { + detail::is_locking() ? vprint_buffered(stdout, fmt.str, va) + : vprint(fmt.str, va); + } } /** @@ -2913,10 +2918,12 @@ FMT_INLINE void print(format_string fmt, T&&... args) { template FMT_INLINE void print(FILE* f, format_string fmt, T&&... args) { vargs va = {{args...}}; - if FMT_CONSTEXPR20 (!detail::use_utf8) - return detail::vprint_mojibake(f, fmt.str, va, false); - detail::is_locking() ? vprint_buffered(f, fmt.str, va) - : vprint(f, fmt.str, va); + if FMT_CONSTEXPR20 (!detail::use_utf8) { + detail::vprint_mojibake(f, fmt.str, va, false); + } else { + detail::is_locking() ? vprint_buffered(f, fmt.str, va) + : vprint(f, fmt.str, va); + } } /// Formats `args` according to specifications in `fmt` and writes the output @@ -2924,8 +2931,11 @@ FMT_INLINE void print(FILE* f, format_string fmt, T&&... args) { template FMT_INLINE void println(FILE* f, format_string fmt, T&&... args) { vargs va = {{args...}}; - if FMT_CONSTEXPR20 (detail::use_utf8) return vprintln(f, fmt.str, va); - detail::vprint_mojibake(f, fmt.str, va, true); + if FMT_CONSTEXPR20 (detail::use_utf8) { + vprintln(f, fmt.str, va); + } else { + detail::vprint_mojibake(f, fmt.str, va, true); + } } /// Formats `args` according to specifications in `fmt` and writes the output From cd67635a19d027d2eae1cd6f0f0fa8f057492907 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 4 Jul 2026 09:57:49 -0700 Subject: [PATCH 2/2] Suppress MSVC C4702 header-wide instead of restructuring code Revert the explicit-else workaround from #4822 and instead disable the "unreachable code" warning for all of base.h. Add an FMT_PRAGMA_MSVC helper (mirroring FMT_PRAGMA_GCC/CLANG) and wrap the header in a single warning(push)/disable:4702/pop pair, keeping the original control flow. --- include/fmt/base.h | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index aca28a8a..f213fa09 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -223,6 +223,11 @@ #else # define FMT_PRAGMA_CLANG(x) #endif +#if FMT_MSC_VERSION +# define FMT_PRAGMA_MSVC(x) __pragma(x) +#else +# define FMT_PRAGMA_MSVC(x) +#endif #ifndef FMT_USE_OPTIMIZE_PRAGMA # define FMT_USE_OPTIMIZE_PRAGMA 1 @@ -235,6 +240,9 @@ FMT_PRAGMA_GCC(push_options) FMT_PRAGMA_GCC(optimize("Og")) #endif +FMT_PRAGMA_MSVC(warning(push)) +FMT_PRAGMA_MSVC(warning(disable : 4702)) + #ifdef FMT_DEPRECATED // Use the provided definition. #elif FMT_HAS_CPP14_ATTRIBUTE(deprecated) @@ -1285,12 +1293,9 @@ constexpr auto to_ascii(Char c) -> char { // Returns the number of code units in a code point or 1 on error. template FMT_CONSTEXPR auto code_point_length(const Char* begin) -> int { - if FMT_CONSTEXPR20 (sizeof(Char) != 1) { - return 1; - } else { - auto c = static_cast(*begin); - return static_cast((0x3a55000000000000ull >> (2 * (c >> 3))) & 3) + 1; - } + if FMT_CONSTEXPR20 (sizeof(Char) != 1) return 1; + auto c = static_cast(*begin); + return static_cast((0x3a55000000000000ull >> (2 * (c >> 3))) & 3) + 1; } // Parses the range [begin, end) as an unsigned integer. This function assumes @@ -2899,12 +2904,10 @@ FMT_API void vprint_buffered(FILE* f, string_view fmt, format_args args); template FMT_INLINE void print(format_string fmt, T&&... args) { vargs va = {{args...}}; - if FMT_CONSTEXPR20 (!detail::use_utf8) { - detail::vprint_mojibake(stdout, fmt.str, va, false); - } else { - detail::is_locking() ? vprint_buffered(stdout, fmt.str, va) - : vprint(fmt.str, va); - } + if FMT_CONSTEXPR20 (!detail::use_utf8) + return detail::vprint_mojibake(stdout, fmt.str, va, false); + detail::is_locking() ? vprint_buffered(stdout, fmt.str, va) + : vprint(fmt.str, va); } /** @@ -2918,12 +2921,10 @@ FMT_INLINE void print(format_string fmt, T&&... args) { template FMT_INLINE void print(FILE* f, format_string fmt, T&&... args) { vargs va = {{args...}}; - if FMT_CONSTEXPR20 (!detail::use_utf8) { - detail::vprint_mojibake(f, fmt.str, va, false); - } else { - detail::is_locking() ? vprint_buffered(f, fmt.str, va) - : vprint(f, fmt.str, va); - } + if FMT_CONSTEXPR20 (!detail::use_utf8) + return detail::vprint_mojibake(f, fmt.str, va, false); + detail::is_locking() ? vprint_buffered(f, fmt.str, va) + : vprint(f, fmt.str, va); } /// Formats `args` according to specifications in `fmt` and writes the output @@ -2931,11 +2932,8 @@ FMT_INLINE void print(FILE* f, format_string fmt, T&&... args) { template FMT_INLINE void println(FILE* f, format_string fmt, T&&... args) { vargs va = {{args...}}; - if FMT_CONSTEXPR20 (detail::use_utf8) { - vprintln(f, fmt.str, va); - } else { - detail::vprint_mojibake(f, fmt.str, va, true); - } + if FMT_CONSTEXPR20 (detail::use_utf8) return vprintln(f, fmt.str, va); + detail::vprint_mojibake(f, fmt.str, va, true); } /// Formats `args` according to specifications in `fmt` and writes the output @@ -2946,6 +2944,7 @@ FMT_INLINE void println(format_string fmt, T&&... args) { } FMT_PRAGMA_GCC(pop_options) +FMT_PRAGMA_MSVC(warning(pop)) FMT_END_EXPORT FMT_END_NAMESPACE