Skip to content

Commit 24f816c

Browse files
committed
fix: better Fn traits formatting
1 parent 4566414 commit 24f816c

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

crates/hir_ty/src/display.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,10 +1164,23 @@ impl HirDisplay for Path {
11641164
// Do we actually format expressions?
11651165
if generic_args.desugared_from_fn {
11661166
// First argument will be a tuple, which already includes the parentheses.
1167-
generic_args.args[0].hir_fmt(f)?;
1167+
// If the tuple only contains 1 item, write it manually to avoid the trailing `,`.
1168+
if let hir_def::path::GenericArg::Type(TypeRef::Tuple(v)) =
1169+
&generic_args.args[0]
1170+
{
1171+
if v.len() == 1 {
1172+
write!(f, "(")?;
1173+
v[0].hir_fmt(f)?;
1174+
write!(f, ")")?;
1175+
} else {
1176+
generic_args.args[0].hir_fmt(f)?;
1177+
}
1178+
}
11681179
if let Some(ret) = &generic_args.bindings[0].type_ref {
1169-
write!(f, " -> ")?;
1170-
ret.hir_fmt(f)?;
1180+
if !matches!(ret, TypeRef::Tuple(v) if v.is_empty()) {
1181+
write!(f, " -> ")?;
1182+
ret.hir_fmt(f)?;
1183+
}
11711184
}
11721185
return Ok(());
11731186
}

0 commit comments

Comments
 (0)