about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJeroen Vannevel <jer_vannevel@outlook.com>2022-02-21 10:29:38 +0000
committerJeroen Vannevel <jer_vannevel@outlook.com>2022-02-21 10:29:38 +0000
commit9c6542f2097df1cfcc9491036ec607c6a2842070 (patch)
treeb1e73e41d1ac06a8c2e9c20f2df31d12046cb0fc
parentd1fc208c9ceb959d616fa790fca5d282bc8d820d (diff)
downloadrust-9c6542f2097df1cfcc9491036ec607c6a2842070.tar.gz
rust-9c6542f2097df1cfcc9491036ec607c6a2842070.zip
parameters.split_last()
-rw-r--r--crates/hir_ty/src/display.rs33
1 files changed, 16 insertions, 17 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index 2ee4f5cf41b..0e75ddeabcd 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -1097,29 +1097,28 @@ impl HirDisplay for TypeRef {
             TypeRef::Fn(parameters, is_varargs) => {
                 // FIXME: Function pointer qualifiers.
                 write!(f, "fn(")?;
-                for index in 0..parameters.len() - 1 {
-                    let (param_name, param_type) = &parameters[index];
-                    if let Some(name) = param_name {
-                        write!(f, "{}: ", name)?;
-                    }
+                if let Some(((_, return_type), function_parameters)) = parameters.split_last() {
+                    for index in 0..function_parameters.len() {
+                        let (param_name, param_type) = &function_parameters[index];
+                        if let Some(name) = param_name {
+                            write!(f, "{}: ", name)?;
+                        }
 
-                    param_type.hir_fmt(f)?;
+                        param_type.hir_fmt(f)?;
 
-                    // Last index contains the return type so we stop writing commas on the second-to-last index
-                    if index != parameters.len() - 2 {
-                        write!(f, ", ")?;
+                        if index != function_parameters.len() - 1 {
+                            write!(f, ", ")?;
+                        }
                     }
-                }
-                if *is_varargs {
-                    write!(f, "{}...", if parameters.len() == 1 { "" } else { ", " })?;
-                }
-                write!(f, ")")?;
-                if let Some((_, ret_ty)) = &parameters.last() {
-                    match ret_ty {
+                    if *is_varargs {
+                        write!(f, "{}...", if parameters.len() == 1 { "" } else { ", " })?;
+                    }
+                    write!(f, ")")?;
+                    match &return_type {
                         TypeRef::Tuple(tup) if tup.is_empty() => {}
                         _ => {
                             write!(f, " -> ")?;
-                            ret_ty.hir_fmt(f)?;
+                            return_type.hir_fmt(f)?;
                         }
                     }
                 }