Skip to content

Commit 7607d16

Browse files
committed
Wrap long array and slice patterns.
Closes rust-lang#4530.
1 parent 7aa69e5 commit 7607d16

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

Diff for: src/overflow.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub(crate) enum OverflowableItem<'a> {
7777
FieldDef(&'a ast::FieldDef),
7878
TuplePatField(&'a TuplePatField<'a>),
7979
Ty(&'a ast::Ty),
80+
Pat(&'a ast::Pat),
8081
}
8182

8283
impl<'a> Rewrite for OverflowableItem<'a> {
@@ -116,6 +117,7 @@ impl<'a> OverflowableItem<'a> {
116117
OverflowableItem::FieldDef(sf) => f(*sf),
117118
OverflowableItem::TuplePatField(pat) => f(*pat),
118119
OverflowableItem::Ty(ty) => f(*ty),
120+
OverflowableItem::Pat(pat) => f(*pat),
119121
}
120122
}
121123

@@ -232,7 +234,7 @@ macro_rules! impl_into_overflowable_item_for_rustfmt_types {
232234
}
233235
}
234236

235-
impl_into_overflowable_item_for_ast_node!(Expr, GenericParam, NestedMetaItem, FieldDef, Ty);
237+
impl_into_overflowable_item_for_ast_node!(Expr, GenericParam, NestedMetaItem, FieldDef, Ty, Pat);
236238
impl_into_overflowable_item_for_rustfmt_types!([MacroArg], [SegmentParam, TuplePatField]);
237239

238240
pub(crate) fn into_overflowable_list<'a, T>(

Diff for: src/patterns.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_span::{BytePos, Span};
44

55
use crate::comment::{combine_strs_with_missing_comments, FindUncommented};
66
use crate::config::lists::*;
7+
use crate::config::Version;
78
use crate::expr::{can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field};
89
use crate::lists::{
910
definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
@@ -231,7 +232,7 @@ impl Rewrite for Pat {
231232
rewrite_tuple_pat(pat_vec, Some(path_str), self.span, context, shape)
232233
}
233234
PatKind::Lit(ref expr) => expr.rewrite(context, shape),
234-
PatKind::Slice(ref slice_pat) => {
235+
PatKind::Slice(ref slice_pat) if context.config.version() == Version::One => {
235236
let rw: Vec<String> = slice_pat
236237
.iter()
237238
.map(|p| {
@@ -244,6 +245,15 @@ impl Rewrite for Pat {
244245
.collect();
245246
Some(format!("[{}]", rw.join(", ")))
246247
}
248+
PatKind::Slice(ref slice_pat) => overflow::rewrite_with_square_brackets(
249+
context,
250+
"",
251+
slice_pat.iter(),
252+
shape,
253+
self.span,
254+
None,
255+
None,
256+
),
247257
PatKind::Struct(ref qself, ref path, ref fields, ellipsis) => {
248258
rewrite_struct_pat(qself, path, fields, ellipsis, self.span, context, shape)
249259
}

Diff for: tests/source/issue-4530.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// rustfmt-version: Two
2+
fn main() {
3+
let [aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccc, ddddddddddddddddddddddddd] = panic!();
4+
}

Diff for: tests/target/issue-4530.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// rustfmt-version: Two
2+
fn main() {
3+
let [
4+
aaaaaaaaaaaaaaaaaaaaaaaaaa,
5+
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
6+
cccccccccccccccccccccccccc,
7+
ddddddddddddddddddddddddd,
8+
] = panic!();
9+
}

0 commit comments

Comments
 (0)