Skip to content

Commit

Permalink
Merge #10823
Browse files Browse the repository at this point in the history
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:
![before](https://user-images.githubusercontent.com/29788806/142745038-44ac68ce-de42-4396-a809-ffdb883be699.png)

after:
![after](https://user-images.githubusercontent.com/29788806/142745040-485feaa2-cc21-4a05-9576-5410ea355029.png)




Co-authored-by: Alex Veber <[email protected]>
  • Loading branch information
bors[bot] and veber-alex authored Nov 21, 2021
2 parents 4566414 + 24f816c commit 183ef04
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions crates/hir_ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,10 +1164,23 @@ 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.
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(());
}
Expand Down

0 comments on commit 183ef04

Please sign in to comment.