Skip to content

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

Closed
@rntz

Description

@rntz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-syntaxextArea: Syntax extensionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.P-lowLow priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions