Skip to content

Commit

Permalink
Auto merge of rust-lang#85272 - ChayimFriedman2:matches-leading-pipe,…
Browse files Browse the repository at this point in the history
… r=m-ou-se

Allow leading pipe in `matches!()` patterns.

This is allowed in `match` statement, and stated in https://internals.rust-lang.org/t/leading-pipe-in-core-matches/14699/2 that it should be allowed in these macros too.
  • Loading branch information
bors committed Aug 2, 2021
2 parents cd5a90f + f10da9f commit 24bbf7a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ macro_rules! assert_ne {
#[allow_internal_unstable(core_panic)]
#[rustc_macro_transparency = "semitransparent"]
pub macro assert_matches {
($left:expr, $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => ({
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => ({
match $left {
$( $pattern )|+ $( if $guard )? => {}
ref left_val => {
Expand All @@ -152,7 +152,7 @@ pub macro assert_matches {
}
}
}),
($left:expr, $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => ({
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => ({
match $left {
$( $pattern )|+ $( if $guard )? => {}
ref left_val => {
Expand Down Expand Up @@ -320,7 +320,7 @@ pub macro debug_assert_matches($($arg:tt)*) {
#[macro_export]
#[stable(feature = "matches_macro", since = "1.42.0")]
macro_rules! matches {
($expression:expr, $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
($expression:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
match $expression {
$( $pattern )|+ $( if $guard )? => true,
_ => false
Expand Down
6 changes: 6 additions & 0 deletions library/core/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ fn assert_escape() {
fn assert_ne_trailing_comma() {
assert_ne!(1, 2,);
}

#[rustfmt::skip]
#[test]
fn matches_leading_pipe() {
matches!(1, | 1 | 2 | 3);
}

0 comments on commit 24bbf7a

Please sign in to comment.