Skip to content

Fix unreachable code warning in range formatter - #4932

Open
Zhao0335 wants to merge 2 commits into
fmtlib:mainfrom
Zhao0335:fix-4930-msvc-unreachable-range-formatter
Open

Fix unreachable code warning in range formatter#4932
Zhao0335 wants to merge 2 commits into
fmtlib:mainfrom
Zhao0335:fix-4930-msvc-unreachable-range-formatter

Conversation

@Zhao0335

@Zhao0335 Zhao0335 commented Sep 6, 2026

Copy link
Copy Markdown

Fixes #4930.

The range formatter constructor returned from a compile-time branch for every
non-set range, leaving the bracket customization after the return. New MSVC
versions diagnose that statement as unreachable when sequence range formatters
are instantiated.

Invert the condition and only customize brackets for set ranges. This preserves
the behavior for both pre-C++20 and C++20 builds while allowing if constexpr
to discard the unused branch.

Testing:

  • MSVC 19.44 /W4: the ranges.h C4702 warning reproduced before the change
    and is absent after it
  • ranges-test passed
  • full MSVC Release build passed
  • full CTest: 20/22 passed; chrono-test.locale and
    unicode-test.legacy_locale fail under this machine's zh-CN locale

@Zhao0335
Zhao0335 requested a review from vitaut as a code owner September 6, 2026 16:55
Comment thread include/fmt/ranges.h Outdated
Comment on lines +518 to +520
if FMT_CONSTEXPR20 (range_format_kind<R, Char>::value == range_format::set)
range_formatter_.set_brackets(detail::string_literal<Char, '{'>{},
detail::string_literal<Char, '}'>{});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is now a multiline-statement, please add braces around the if body.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added braces around the if body in af21761.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unreachable code warning in ranges.h for msvc with /W4

2 participants