Repository: rust-lang/rustfmt (rust)

Issue:
Long slice patterns are not getting wrapped
It seems that rustfmt doesn't wrap long slice pattern lines.

**Input**

```rust
enum Foo {
    IAmALongVariantNameIJustKeepGoingOn { a: usize, b: usize },
}

fn main() {
    let foos = vec![Foo::IAmALongVariantNameIJustKeepGoingOn { a: 0, b: 0 }];

    match foos.as_slice() {
        [
            Foo::IAmALongVariantNameIJustKeepGoingOn { a: 0, b: 0 },
            Foo::IAmALongVariantNameIJustKeepGoingOn { a: 0, b: 0 },
            Foo::IAmALongVariantNameIJustKeepGoingOn { a: 0, b: 0 },
        ] => {}
        _ => {}
    }
}

```

**Output**

```rust
enum Foo {
    IAmALongVariantNameIJustKeepGoingOn { a: usize, b: usize },
}

fn main() {
    let foos = vec![Foo::IAmALongVariantNameIJustKeepGoingOn { a: 0, b: 0 }];

    match foos.as_slice() {
        [Foo::IAmALongVariantNameIJustKeepGoingOn { a: 0, b: 0 }, Foo::IAmALongVariantNameIJustKeepGoingOn { a: 0, b: 0 }, Foo::IAmALongVariantNameIJustKeepGoingOn { a: 0, b: 0 }] =>
            {}
        _ => {}
    }
}

```

**Expected output**

```rust
enum Foo {
    IAmALongVariantNameIJustKeepGoingOn { a: usize, b: usize },
}

fn main() {
    let foos = vec![Foo::IAmALongVariantNameIJustKeepGoingOn { a: 0, b: 0 }];

    match foos.as_slice() {
        [
            Foo::IAmALongVariantNameIJustKeepGoingOn { a: 0, b: 0 },
            Foo::IAmALongVariantNameIJustKeepGoingOn { a: 0, b: 0 },
            Foo::IAmALongVariantNameIJustKeepGoingOn { a: 0, b: 0 },
        ] => {}
        _ => {}
    }
}

```

**Meta**

- rustfmt version: rustfmt 1.4.26-nightly (293d7d01 2020-11-14) and rustfmt 2.0.0-rc.2-nightly (8d686523 2020-11-14)
- From where did you install rustfmt?: rustup, and built by hand

Pull request description:
Wrap long array and slice patterns.
Closes #4530.

Patch:
diff --git a/src/overflow.rs b/src/overflow.rs
index e3221346..ac24181c 100644
--- a/src/overflow.rs
+++ b/src/overflow.rs
@@ -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> {
@@ -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),
         }
     }
 
@@ -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>(
diff --git a/src/patterns.rs b/src/patterns.rs
index 4c6a2d5d..34987b1d 100644
--- a/src/patterns.rs
+++ b/src/patterns.rs
@@ -4,6 +4,7 @@ use rustc_span::{BytePos, Span};
 
 use crate::comment::{combine_strs_with_missing_comments, FindUncommented};
 use crate::config::lists::*;
+use crate::config::Version;
 use crate::expr::{can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field};
 use crate::lists::{
     definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
@@ -232,7 +233,7 @@ impl Rewrite for Pat {
                 rewrite_tuple_pat(pat_vec, Some(path_str), self.span, context, shape)
             }
             PatKind::Lit(ref expr) => expr.rewrite(context, shape),
-            PatKind::Slice(ref slice_pat) => {
+            PatKind::Slice(ref slice_pat) if context.config.version() == Version::One => {
                 let rw: Vec<String> = slice_pat
                     .iter()
                     .map(|p| {
@@ -245,6 +246,15 @@ impl Rewrite for Pat {
                     .collect();
                 Some(format!("[{}]", rw.join(", ")))
             }
+            PatKind::Slice(ref slice_pat) => 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)
             }


Test patch:
diff --git a/tests/source/issue-4530.rs b/tests/source/issue-4530.rs
new file mode 100644
index 00000000..9d2882ab
--- /dev/null
+++ b/tests/source/issue-4530.rs
@@ -0,0 +1,4 @@
+// rustfmt-version: Two
+fn main() {
+    let [aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccccc, ddddddddddddddddddddddddd] = panic!();
+}
diff --git a/tests/target/issue-4530.rs b/tests/target/issue-4530.rs
new file mode 100644
index 00000000..296dc559
--- /dev/null
+++ b/tests/target/issue-4530.rs
@@ -0,0 +1,9 @@
+// rustfmt-version: Two
+fn main() {
+    let [
+        aaaaaaaaaaaaaaaaaaaaaaaaaa,
+        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
+        cccccccccccccccccccccccccc,
+        ddddddddddddddddddddddddd,
+    ] = panic!();
+}