Skip to content

fix: explicitly mark unreachable code to prevent GCC warnings#642

Closed
mhx wants to merge 1 commit into
facebook:mainfrom
mhx:mhx/assume-unreachable
Closed

fix: explicitly mark unreachable code to prevent GCC warnings#642
mhx wants to merge 1 commit into
facebook:mainfrom
mhx:mhx/assume-unreachable

Conversation

@mhx

@mhx mhx commented Mar 15, 2025

Copy link
Copy Markdown
Contributor

Currently, GCC warns about code that switches on an enum, but despite handling all enumerations, will not return a value in the default case.

thrift/compiler/sema/check_map_keys.cc: In function 'bool apache::thrift::compiler::{anonymous}::lt_value(const apache::thrift::compiler::t_const_value*, const apache::thrift::compiler::t_const_value*)':
thrift/compiler/sema/check_map_keys.cc:167:1: warning: control reaches end of non-void function [-Wreturn-type]
  167 | }
      | ^

This change adds abort() after each of these switch statement, the same approach as used e.g. in t_const_value::kind_to_string.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@createdbysk has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@vitaut vitaut left a comment

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.

Thanks for the PR! Introducing assume_unreachable seems like an overkill. Let's use std::terminate or throw std::logic_error instead.

Currently, GCC warns about code that switches on an enum, but despite
handling all enumerations, will not return a value in the default case.

    thrift/compiler/sema/check_map_keys.cc: In function 'bool apache::thrift::compiler::{anonymous}::lt_value(const apache::thrift::compiler::t_const_value*, const apache::thrift::compiler::t_const_value*)':
    thrift/compiler/sema/check_map_keys.cc:167:1: warning: control reaches end of non-void function [-Wreturn-type]
      167 | }
          | ^

This change adds `abort()` after each of these switch statement, the
same approach as used e.g. in `t_const_value::kind_to_string`.
@mhx mhx force-pushed the mhx/assume-unreachable branch from f1b961a to 68b62cc Compare March 19, 2025 07:00
@mhx

mhx commented Mar 19, 2025

Copy link
Copy Markdown
Contributor Author

Thanks for the PR! Introducing assume_unreachable seems like an overkill. Let's use std::terminate or throw std::logic_error instead.

That's fair. I've actually found this code, which uses abort() and changed this PR to do the same.

@mhx mhx requested a review from vitaut March 19, 2025 07:02

@vitaut vitaut left a comment

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.

LGTM

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@createdbysk has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@createdbysk merged this pull request in e462db1.

facebook-github-bot pushed a commit to facebook/hhvm that referenced this pull request Mar 20, 2025
Summary:
Currently, GCC warns about code that switches on an enum, but despite handling all enumerations, will not return a value in the default case.

    thrift/compiler/sema/check_map_keys.cc: In function 'bool apache::thrift::compiler::{anonymous}::lt_value(const apache::thrift::compiler::t_const_value*, const apache::thrift::compiler::t_const_value*)':
    thrift/compiler/sema/check_map_keys.cc:167:1: warning: control reaches end of non-void function [-Wreturn-type]
      167 | }
          | ^

This change adds `abort()` after each of these switch statement, the same approach as used e.g. in `t_const_value::kind_to_string`.

X-link: facebook/fbthrift#642

Reviewed By: vitaut

Differential Revision: D71357532

Pulled By: createdbysk

fbshipit-source-id: ec1a29e74429f7db83f9fb7b811bee06e238679d
@vitaut

vitaut commented Mar 20, 2025

Copy link
Copy Markdown
Contributor

Merged, thanks!

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

3 participants