Skip to content

Silence unreachable code warnings in MSVC when FMT_USE_EXCEPTIONS are… #4515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2025

Conversation

mattiasljungstrom
Copy link
Contributor

Removes warnings about unreachable code in VS 2022, 17.14.12 when exceptions are disabled.

@vitaut
Copy link
Contributor

vitaut commented Aug 14, 2025

Please post the full warning.

Comment on lines +142 to +143
volatile bool b = true;
if (b) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this can be

if (const_check(true)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried this version and it unfortunately doesn't stop the warnings:

FMT_FUNC void report_error(const char* message) {
#if FMT_USE_EXCEPTIONS
  // Use FMT_THROW instead of throw to avoid bogus unreachable code warnings
  // from MSVC.
  FMT_THROW(format_error(message));
#else
  if (detail::const_check(true)) {
    fputs(message, stderr);
    abort();
  }
#endif
}

@mattiasljungstrom
Copy link
Contributor Author

Please post the full warning.

Basically every return that happens after the call to report_error():

fmt\include\fmt\format.h(3522): warning C4702: unreachable code
fmt\include\fmt\format.h(2192): warning C4702: unreachable code
fmt\include\fmt\base.h(901): warning C4702: unreachable code
fmt\include\fmt\base.h(901): warning C4702: unreachable code
fmt\include\fmt\format.h(3439): warning C4702: unreachable code
fmt\include\fmt\base.h(1433): warning C4702: unreachable code
fmt\include\fmt\base.h(1563): warning C4702: unreachable code
fmt\include\fmt\base.h(1567): warning C4702: unreachable code
fmt\include\fmt\base.h(1349): warning C4702: unreachable code
fmt\include\fmt\base.h(1590): warning C4702: unreachable code
fmt\include\fmt\base.h(1618): warning C4702: unreachable code
fmt\include\fmt\base.h(1625): warning C4702: unreachable code

@vitaut
Copy link
Contributor

vitaut commented Aug 17, 2025

Could you provide a gobolt repro? I tried to repro in https://www.godbolt.org/z/Mjb6xY4vT but to no avail.

@mattiasljungstrom
Copy link
Contributor Author

Could you provide a gobolt repro? I tried to repro in https://www.godbolt.org/z/Mjb6xY4vT but to no avail.

It seems to only trigger in release mode, and with /W4 /O2

https://www.godbolt.org/z/YfPYK6334

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

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

In general we only care about /W3 but I guess this one is trivial to fix so let's merge.

@vitaut vitaut merged commit 9588458 into fmtlib:master Aug 17, 2025
41 checks passed
@vitaut
Copy link
Contributor

vitaut commented Aug 17, 2025

Merged, thanks.

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.

2 participants