mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 11:07:42 +00:00
Merge #10823
10823: fix: better `Fn` traits formatting r=jonas-schievink a=veber-alex This makes it so formatting `Fn` traits properly handles: 1. An `Fn` trait with only a single argument -> removes the trailing comma. 2. An `Fn` trait which returns nothing (an empty tuple) -> don't show `-> ()` as the return type. before:  after:  Co-authored-by: Alex Veber <alexveber@gmail.com>
This commit is contained in:
commit
183ef048f6
@ -1164,11 +1164,24 @@ impl HirDisplay for Path {
|
||||
// Do we actually format expressions?
|
||||
if generic_args.desugared_from_fn {
|
||||
// First argument will be a tuple, which already includes the parentheses.
|
||||
// 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 {
|
||||
if !matches!(ret, TypeRef::Tuple(v) if v.is_empty()) {
|
||||
write!(f, " -> ")?;
|
||||
ret.hir_fmt(f)?;
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user