Skip to content

Commit 24bbf7a

Browse files
committed
Auto merge of #85272 - ChayimFriedman2:matches-leading-pipe, 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.
2 parents cd5a90f + f10da9f commit 24bbf7a

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
@@ -140,7 +140,7 @@ macro_rules! assert_ne {
140140
#[allow_internal_unstable(core_panic)]
141141
#[rustc_macro_transparency = "semitransparent"]
142142
pub macro assert_matches {
143-
($left:expr, $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => ({
143+
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => ({
144144
match $left {
145145
$( $pattern )|+ $( if $guard )? => {}
146146
ref left_val => {
@@ -152,7 +152,7 @@ pub macro assert_matches {
152152
}
153153
}
154154
}),
155-
($left:expr, $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => ({
155+
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => ({
156156
match $left {
157157
$( $pattern )|+ $( if $guard )? => {}
158158
ref left_val => {
@@ -320,7 +320,7 @@ pub macro debug_assert_matches($($arg:tt)*) {
320320
#[macro_export]
321321
#[stable(feature = "matches_macro", since = "1.42.0")]
322322
macro_rules! matches {
323-
($expression:expr, $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
323+
($expression:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
324324
match $expression {
325325
$( $pattern )|+ $( if $guard )? => true,
326326
_ => 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)