Skip to content

Commit

Permalink
Wrap long array and slice patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed Sep 18, 2021
1 parent 7aa69e5 commit cd09303
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub(crate) enum OverflowableItem<'a> {
FieldDef(&'a ast::FieldDef),
TuplePatField(&'a TuplePatField<'a>),
Ty(&'a ast::Ty),
Pat(&'a ast::Pat),
}

impl<'a> Rewrite for OverflowableItem<'a> {
Expand Down Expand Up @@ -116,6 +117,7 @@ impl<'a> OverflowableItem<'a> {
OverflowableItem::FieldDef(sf) => f(*sf),
OverflowableItem::TuplePatField(pat) => f(*pat),
OverflowableItem::Ty(ty) => f(*ty),
OverflowableItem::Pat(pat) => f(*pat),
}
}

Expand Down Expand Up @@ -232,7 +234,7 @@ macro_rules! impl_into_overflowable_item_for_rustfmt_types {
}
}

impl_into_overflowable_item_for_ast_node!(Expr, GenericParam, NestedMetaItem, FieldDef, Ty);
impl_into_overflowable_item_for_ast_node!(Expr, GenericParam, NestedMetaItem, FieldDef, Ty, Pat);
impl_into_overflowable_item_for_rustfmt_types!([MacroArg], [SegmentParam, TuplePatField]);

pub(crate) fn into_overflowable_list<'a, T>(
Expand Down
20 changes: 9 additions & 11 deletions src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,15 @@ impl Rewrite for Pat {
}
PatKind::Lit(ref expr) => expr.rewrite(context, shape),
PatKind::Slice(ref slice_pat) => {
let rw: Vec<String> = slice_pat
.iter()
.map(|p| {
if let Some(rw) = p.rewrite(context, shape) {
rw
} else {
context.snippet(p.span).to_string()
}
})
.collect();
Some(format!("[{}]", rw.join(", ")))
overflow::rewrite_with_square_brackets(
context,
"",
slice_pat.iter(),
shape,
self.span,
None,
None,
)
}
PatKind::Struct(ref qself, ref path, ref fields, ellipsis) => {
rewrite_struct_pat(qself, path, fields, ellipsis, self.span, context, shape)
Expand Down
3 changes: 3 additions & 0 deletions tests/source/issue-4530.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
let [aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccc, ddddddddddddddddddddddddd] = panic!();
}
8 changes: 8 additions & 0 deletions tests/target/issue-4530.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let [
aaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
cccccccccccccccccccccccccc,
ddddddddddddddddddddddddd,
] = panic!();
}

0 comments on commit cd09303

Please sign in to comment.