]> git.lizzy.rs Git - rust.git/commitdiff
Use `split_{first,last}` in `cs_fold1`.
authorNicholas Nethercote <n.nethercote@gmail.com>
Tue, 28 Jun 2022 02:58:36 +0000 (12:58 +1000)
committerNicholas Nethercote <n.nethercote@gmail.com>
Thu, 30 Jun 2022 20:04:36 +0000 (06:04 +1000)
It makes the code a little nicer to read.

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

index 8347cded2fe0bb0adb9913421ea3bf0778b9b4cc..e6b21b3ff944e2916cc88459103653cc9f6397ce 100644 (file)
@@ -1717,22 +1717,21 @@ pub fn cs_fold1<F, B>(
 {
     match *substructure.fields {
         EnumMatching(.., ref all_fields) | Struct(_, ref all_fields) => {
-            let (base, all_fields) = match (all_fields.is_empty(), use_foldl) {
+            let (base, rest) = match (all_fields.is_empty(), use_foldl) {
                 (false, true) => {
-                    let field = &all_fields[0];
-                    let args = (field.span, field.self_.clone(), &field.other[..]);
-                    (b(cx, Some(args)), &all_fields[1..])
+                    let (first, rest) = all_fields.split_first().unwrap();
+                    let args = (first.span, first.self_.clone(), &first.other[..]);
+                    (b(cx, Some(args)), rest)
                 }
                 (false, false) => {
-                    let idx = all_fields.len() - 1;
-                    let field = &all_fields[idx];
-                    let args = (field.span, field.self_.clone(), &field.other[..]);
-                    (b(cx, Some(args)), &all_fields[..idx])
+                    let (last, rest) = all_fields.split_last().unwrap();
+                    let args = (last.span, last.self_.clone(), &last.other[..]);
+                    (b(cx, Some(args)), rest)
                 }
                 (true, _) => (b(cx, None), &all_fields[..]),
             };
 
-            cs_fold_fields(use_foldl, f, base, cx, all_fields)
+            cs_fold_fields(use_foldl, f, base, cx, rest)
         }
         EnumNonMatchingCollapsed(..) => {
             cs_fold_enumnonmatch(enum_nonmatch_f, cx, trait_span, substructure)