Skip to content

Commit 2c4abbb

Browse files
Format trailing where clauses in rustfmt
1 parent df2471b commit 2c4abbb

File tree

3 files changed

+97
-19
lines changed

3 files changed

+97
-19
lines changed

src/items.rs

+57-19
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,9 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
16571657
where_predicates_split,
16581658
} = *ty_alias_kind;
16591659
let ty_opt = ty.as_ref();
1660+
let rhs_hi = ty
1661+
.as_ref()
1662+
.map_or(where_clauses.0.1.hi(), |ty| ty.span.hi());
16601663
let (ident, vis) = match visitor_kind {
16611664
Item(i) => (i.ident, &i.vis),
16621665
AssocTraitItem(i) | AssocImplItem(i) => (i.ident, &i.vis),
@@ -1679,17 +1682,23 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
16791682
match (visitor_kind, &op_ty) {
16801683
(Item(_) | AssocTraitItem(_) | ForeignItem(_), Some(op_bounds)) => {
16811684
let op = OpaqueType { bounds: op_bounds };
1682-
rewrite_ty(rw_info, Some(bounds), Some(&op), vis)
1685+
rewrite_ty(rw_info, Some(bounds), Some(&op), rhs_hi, vis)
16831686
}
16841687
(Item(_) | AssocTraitItem(_) | ForeignItem(_), None) => {
1685-
rewrite_ty(rw_info, Some(bounds), ty_opt, vis)
1688+
rewrite_ty(rw_info, Some(bounds), ty_opt, rhs_hi, vis)
16861689
}
16871690
(AssocImplItem(_), _) => {
16881691
let result = if let Some(op_bounds) = op_ty {
16891692
let op = OpaqueType { bounds: op_bounds };
1690-
rewrite_ty(rw_info, Some(bounds), Some(&op), &DEFAULT_VISIBILITY)
1693+
rewrite_ty(
1694+
rw_info,
1695+
Some(bounds),
1696+
Some(&op),
1697+
rhs_hi,
1698+
&DEFAULT_VISIBILITY,
1699+
)
16911700
} else {
1692-
rewrite_ty(rw_info, Some(bounds), ty_opt, vis)
1701+
rewrite_ty(rw_info, Some(bounds), ty_opt, rhs_hi, vis)
16931702
}?;
16941703
match defaultness {
16951704
ast::Defaultness::Default(..) => Some(format!("default {result}")),
@@ -1703,6 +1712,8 @@ fn rewrite_ty<R: Rewrite>(
17031712
rw_info: &TyAliasRewriteInfo<'_, '_>,
17041713
generic_bounds_opt: Option<&ast::GenericBounds>,
17051714
rhs: Option<&R>,
1715+
// the span of the end of the RHS (or the end of the generics, if there is no RHS)
1716+
rhs_hi: BytePos,
17061717
vis: &ast::Visibility,
17071718
) -> Option<String> {
17081719
let mut result = String::with_capacity(128);
@@ -1719,9 +1730,6 @@ fn rewrite_ty<R: Rewrite>(
17191730
.where_clause
17201731
.predicates
17211732
.split_at(where_predicates_split);
1722-
if !after_where_predicates.is_empty() {
1723-
return None;
1724-
}
17251733
result.push_str(&format!("{}type ", format_visibility(context, vis)));
17261734
let ident_str = rewrite_ident(context, ident);
17271735

@@ -1750,7 +1758,7 @@ fn rewrite_ty<R: Rewrite>(
17501758
if rhs.is_none() {
17511759
option.suppress_comma();
17521760
}
1753-
let where_clause_str = rewrite_where_clause(
1761+
let before_where_clause_str = rewrite_where_clause(
17541762
context,
17551763
before_where_predicates,
17561764
where_clauses.0.1,
@@ -1762,14 +1770,20 @@ fn rewrite_ty<R: Rewrite>(
17621770
generics.span.hi(),
17631771
option,
17641772
)?;
1765-
result.push_str(&where_clause_str);
1773+
result.push_str(&before_where_clause_str);
17661774

1767-
if let Some(ty) = rhs {
1768-
// If there's a where clause, add a newline before the assignment. Otherwise just add a
1769-
// space.
1770-
let has_where = !before_where_predicates.is_empty();
1771-
if has_where {
1775+
let mut result = if let Some(ty) = rhs {
1776+
// If there are any where clauses, add a newline before the assignment.
1777+
// If there is a before where clause, do not indent, but if there is
1778+
// only an after where clause, additionally indent the type.
1779+
if !before_where_predicates.is_empty() {
17721780
result.push_str(&indent.to_string_with_newline(context.config));
1781+
} else if !after_where_predicates.is_empty() {
1782+
result.push_str(
1783+
&indent
1784+
.block_indent(context.config)
1785+
.to_string_with_newline(context.config),
1786+
);
17731787
} else {
17741788
result.push(' ');
17751789
}
@@ -1783,7 +1797,7 @@ fn rewrite_ty<R: Rewrite>(
17831797
Some(comment_span)
17841798
if contains_comment(context.snippet_provider.span_to_snippet(comment_span)?) =>
17851799
{
1786-
let comment_shape = if has_where {
1800+
let comment_shape = if !before_where_predicates.is_empty() {
17871801
Shape::indented(indent, context.config)
17881802
} else {
17891803
Shape::indented(indent, context.config)
@@ -1802,12 +1816,36 @@ fn rewrite_ty<R: Rewrite>(
18021816
_ => format!("{result}="),
18031817
};
18041818

1805-
// 1 = `;`
1806-
let shape = Shape::indented(indent, context.config).sub_width(1)?;
1807-
rewrite_assign_rhs(context, lhs, &*ty, &RhsAssignKind::Ty, shape).map(|s| s + ";")
1819+
// 1 = `;` unless there's a trailing where clause
1820+
let shape = if after_where_predicates.is_empty() {
1821+
Shape::indented(indent, context.config).sub_width(1)?
1822+
} else {
1823+
Shape::indented(indent, context.config)
1824+
};
1825+
rewrite_assign_rhs(context, lhs, &*ty, &RhsAssignKind::Ty, shape)?
18081826
} else {
1809-
Some(format!("{result};"))
1827+
result
1828+
};
1829+
1830+
if !after_where_predicates.is_empty() {
1831+
let option = WhereClauseOption::new(true, WhereClauseSpace::Newline);
1832+
let after_where_clause_str = rewrite_where_clause(
1833+
context,
1834+
after_where_predicates,
1835+
where_clauses.1.1,
1836+
context.config.brace_style(),
1837+
Shape::indented(indent, context.config),
1838+
false,
1839+
";",
1840+
None,
1841+
rhs_hi,
1842+
option,
1843+
)?;
1844+
result.push_str(&after_where_clause_str);
18101845
}
1846+
1847+
result += ";";
1848+
Some(result)
18111849
}
18121850

18131851
fn type_annotation_spacing(config: &Config) -> (&str, &str) {
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type Foo
2+
where
3+
A: B,
4+
C: D,
5+
= E;
6+
7+
type Foo
8+
where
9+
A: B,
10+
C: D,
11+
= E
12+
where
13+
F: G,
14+
H: I;
15+
16+
type Foo
17+
= E
18+
where
19+
F: G,
20+
H: I;
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type Foo
2+
where
3+
A: B,
4+
C: D,
5+
= E;
6+
7+
type Foo
8+
where
9+
A: B,
10+
C: D,
11+
= E
12+
where
13+
F: G,
14+
H: I;
15+
16+
type Foo
17+
= E
18+
where
19+
F: G,
20+
H: I;

0 commit comments

Comments
 (0)