Skip to content

Commit f10da9f

Browse files
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.
1 parent 0a6c636 commit f10da9f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

library/core/src/macros/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ macro_rules! assert_ne {
141141
#[allow_internal_unstable(core_panic)]
142142
#[rustc_macro_transparency = "semitransparent"]
143143
pub macro assert_matches {
144-
($left:expr, $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => ({
144+
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => ({
145145
match $left {
146146
$( $pattern )|+ $( if $guard )? => {}
147147
ref left_val => {
@@ -153,7 +153,7 @@ pub macro assert_matches {
153153
}
154154
}
155155
}),
156-
($left:expr, $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => ({
156+
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => ({
157157
match $left {
158158
$( $pattern )|+ $( if $guard )? => {}
159159
ref left_val => {
@@ -321,7 +321,7 @@ pub macro debug_assert_matches($($arg:tt)*) {
321321
#[macro_export]
322322
#[stable(feature = "matches_macro", since = "1.42.0")]
323323
macro_rules! matches {
324-
($expression:expr, $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
324+
($expression:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
325325
match $expression {
326326
$( $pattern )|+ $( if $guard )? => true,
327327
_ => false

library/core/tests/macros.rs

+6
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ fn assert_escape() {
1212
fn assert_ne_trailing_comma() {
1313
assert_ne!(1, 2,);
1414
}
15+
16+
#[rustfmt::skip]
17+
#[test]
18+
fn matches_leading_pipe() {
19+
matches!(1, | 1 | 2 | 3);
20+
}

0 commit comments

Comments
 (0)