Skip to content

Attribute(?) to selectively disable "unreachable pattern" errors #4112

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

Closed
rntz opened this issue Dec 4, 2012 · 11 comments
Closed

Attribute(?) to selectively disable "unreachable pattern" errors #4112

rntz opened this issue Dec 4, 2012 · 11 comments
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one. P-low Low priority

Comments

@rntz
Copy link
Contributor

rntz commented Dec 4, 2012

When writing macros that expand to "match" expressions, it can be hard to guarantee that all cases of the match will be necessary. Consider the following macro:

macro_rules! case(
  ($e:expr, $p:pat where $cond:expr => $thn:expr, $els:expr) => (
    match $e {
      $p => if $cond { $thn } else { $els },
      _ => $els
    }
  )
)

An example use of this macro would be case!(some_expr, x where x > 3 => x, 3), which is equivalent to max(3, some_expr) (a silly example, but still).

Unfortunately, this example fails to compile because it expands to

match some_expr {
  x => if x > 3 { x } else { 3 },
  _ => 3
}

The last case of the match is redundant. However, there are also uses of the macro where the last case will not be redundant, eg. case!(some_expr, [email protected] where x % 2 == 0 => true, false).

It would be nice if there were some way to mark that a particular match expression is allowed to have unreachable cases, or (better yet) that a particular case was allowed to be unreachable.

@catamorphism
Copy link
Contributor

Nominating for milestone 3, feature-complete

@graydon
Copy link
Contributor

graydon commented May 1, 2013

this would just be a scoped lint, right? #[allow(unreachable_pattern)] like?

@catamorphism
Copy link
Contributor

I think this was never actually discussed, so, re-labelling it as "nominated".

@emberian
Copy link
Member

Still relevant.

@graydon
Copy link
Contributor

graydon commented Jul 18, 2013

accepted for feature-complete milestone

@pnkfelix
Copy link
Member

pnkfelix commented Feb 6, 2014

Not a 1.0 blocker, P-low.

@pnkfelix pnkfelix added P-low and removed P-high labels Feb 6, 2014
@huonw
Copy link
Member

huonw commented Aug 4, 2014

Triage, no change.

@steveklabnik
Copy link
Member

Triage: no change

@jonas-schievink
Copy link
Contributor

To anyone needing this now:

match 0 {
    1 ... 3 => {}
    x if true => {}
    _ => {}
}

@tbu-
Copy link
Contributor

tbu- commented Dec 1, 2015

There's an easy work-around, don't think an attribute is necessary here.

@steveklabnik
Copy link
Member

Since new lints have a big impact on users of rustc, the policy is that they should go through the RFC process like other user-facing changes. As such, I'm going to give this one a close, but if anyone comes across this ticket and wants this lint, consider adding it to clippy and/or writing up an RFC. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one. P-low Low priority
Projects
None yet
Development

No branches or pull requests

9 participants