Skip to content
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

warn(unreachable_code) on empty error enums #476

Open
Kyuuhachi opened this issue Jan 12, 2025 · 3 comments
Open

warn(unreachable_code) on empty error enums #476

Kyuuhachi opened this issue Jan 12, 2025 · 3 comments

Comments

@Kyuuhachi
Copy link

Using an empty enum as source produces an unreachable_code warning. Which is certainly true, but should it do that? Sometimes you want to return a Result for forward compatibility or whatever.

use snafu::ResultExt as _;

#[derive(Debug, snafu::Snafu)]
enum Outer {
	Inner { source: Inner },
}

#[derive(Debug, snafu::Snafu)]
enum Inner {}

fn infallible() -> Result<(), Inner> {
	Ok(())
}

fn main() -> Result<(), Outer> {
	infallible().context(InnerSnafu)
}
warning: unreachable expression
 --> src/main.rs:3:17
  |
3 | #[derive(Debug, snafu::Snafu)]
  |                 ^^^^^^^^^^^^
  |                 |
  |                 unreachable expression
  |                 any code following this expression is unreachable
  |
note: this expression has type `Inner`, which is uninhabited
 --> src/main.rs:3:17
  |
3 | #[derive(Debug, snafu::Snafu)]
  |                 ^^^^^^^^^^^^
  = note: `#[warn(unreachable_code)]` on by default
  = note: this warning originates in the derive macro `snafu::Snafu` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `snafu-unreachable` (bin "snafu-unreachable") generated 1 warning
@Enet4
Copy link
Collaborator

Enet4 commented Jan 13, 2025

Would you be able to use Infallible instead of declaring Inner?

@Kyuuhachi
Copy link
Author

That gives the same error, but is less forward compatible.

@Stargateur
Copy link
Contributor

Stargateur commented Jan 13, 2025

if Inner is hidden from user as its seem to be the thing you want why not use empty struct ? or even nothing inside Inner variant.

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

No branches or pull requests

3 participants