From 24f816c4815b02327c31574685bf80e78d2b9743 Mon Sep 17 00:00:00 2001 From: Alex Veber Date: Sun, 21 Nov 2021 02:39:22 +0200 Subject: [PATCH] fix: better `Fn` traits formatting --- crates/hir_ty/src/display.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index d1ca50c6909..e2424dd6d9e 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -1164,10 +1164,23 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { // Do we actually format expressions? if generic_args.desugared_from_fn { // First argument will be a tuple, which already includes the parentheses. - generic_args.args[0].hir_fmt(f)?; + // If the tuple only contains 1 item, write it manually to avoid the trailing `,`. + if let hir_def::path::GenericArg::Type(TypeRef::Tuple(v)) = + &generic_args.args[0] + { + if v.len() == 1 { + write!(f, "(")?; + v[0].hir_fmt(f)?; + write!(f, ")")?; + } else { + generic_args.args[0].hir_fmt(f)?; + } + } if let Some(ret) = &generic_args.bindings[0].type_ref { - write!(f, " -> ")?; - ret.hir_fmt(f)?; + if !matches!(ret, TypeRef::Tuple(v) if v.is_empty()) { + write!(f, " -> ")?; + ret.hir_fmt(f)?; + } } return Ok(()); }