Skip to content

Commit

Permalink
Only treat plain literal patterns as short
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jan 8, 2025
1 parent 1f81f90 commit a8e00b0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/tools/rustfmt/src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ pub(crate) fn is_short_pattern(pat: &ast::Pat, pat_str: &str) -> bool {
}

fn is_short_pattern_inner(pat: &ast::Pat) -> bool {
match pat.kind {
ast::PatKind::Rest
| ast::PatKind::Never
| ast::PatKind::Wild
| ast::PatKind::Err(_)
| ast::PatKind::Lit(_) => true,
match &pat.kind {
ast::PatKind::Rest | ast::PatKind::Never | ast::PatKind::Wild | ast::PatKind::Err(_) => {
true
}
ast::PatKind::Lit(expr) => match &expr.kind {
ast::ExprKind::Lit(_) => true,
ast::ExprKind::Unary(ast::UnOp::Neg, expr) => match &expr.kind {
ast::ExprKind::Lit(_) => true,
_ => unreachable!(),
},
ast::ExprKind::ConstBlock(_) => false,
ast::ExprKind::Path(..) => false,
_ => unreachable!(),
},
ast::PatKind::Ident(_, _, ref pat) => pat.is_none(),
ast::PatKind::Struct(..)
| ast::PatKind::MacCall(..)
Expand Down
10 changes: 10 additions & 0 deletions src/tools/rustfmt/tests/source/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,13 @@ fn issue3728() {
| c;
foo((1,));
}

fn literals() {
match 42 {
const { 1 + 2 } | 4
| 6 => {}
10 | 11 | 12
| 13 | 14 => {}
_ => {}
}
}
8 changes: 8 additions & 0 deletions src/tools/rustfmt/tests/target/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,11 @@ fn issue3728() {
let foo = |(c,)| c;
foo((1,));
}

fn literals() {
match 42 {
const { 1 + 2 } | 4 | 6 => {}
10 | 11 | 12 | 13 | 14 => {}
_ => {}
}
}

0 comments on commit a8e00b0

Please sign in to comment.