Skip to content

Commit 8073aab

Browse files
authored
Rollup merge of #133424 - Nadrieril:guard-patterns-parsing, r=fee1-dead
Parse guard patterns This implements the parsing of [RFC3637 Guard Patterns](https://rust-lang.github.io/rfcs/3637-guard-patterns.html) (see also [tracking issue](rust-lang/rust#129967)). This PR is extracted from rust-lang/rust#129996 with minor modifications. cc `@max-niederman`
2 parents 232528a + 74dceee commit 8073aab

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/patterns.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ fn is_short_pattern_inner(pat: &ast::Pat) -> bool {
4848
| ast::PatKind::MacCall(..)
4949
| ast::PatKind::Slice(..)
5050
| ast::PatKind::Path(..)
51-
| ast::PatKind::Range(..) => false,
51+
| ast::PatKind::Range(..)
52+
| ast::PatKind::Guard(..) => false,
5253
ast::PatKind::Tuple(ref subpats) => subpats.len() <= 1,
5354
ast::PatKind::TupleStruct(_, ref path, ref subpats) => {
5455
path.segments.len() <= 1 && subpats.len() <= 1
@@ -338,8 +339,9 @@ impl Rewrite for Pat {
338339
.max_width_error(shape.width, self.span)?,
339340
)
340341
.map(|inner_pat| format!("({})", inner_pat)),
341-
PatKind::Err(_) => Err(RewriteError::Unknown),
342+
PatKind::Guard(..) => Ok(context.snippet(self.span).to_string()),
342343
PatKind::Deref(_) => Err(RewriteError::Unknown),
344+
PatKind::Err(_) => Err(RewriteError::Unknown),
343345
}
344346
}
345347
}

tests/target/guard_patterns.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(guard_patterns)]
2+
3+
fn main() {
4+
match user.subscription_plan() {
5+
(Plan::Regular if user.credit() >= 100) | (Plan::Premium if user.credit() >= 80) => {
6+
// Complete the transaction.
7+
}
8+
_ => {
9+
// The user doesn't have enough credit, return an error message.
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)