Skip to content

Commit c5a5db2

Browse files
authored
Merge pull request rust-lang#1930 from topecongiro/poor/item_brace_style
Add struct_remove_empty_body config option
2 parents 8122f0b + 4ad81d0 commit c5a5db2

3 files changed

+31
-87
lines changed

src/items.rs

+23-87
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,6 @@ impl<'a> FmtVisitor<'a> {
387387
Some(ref body_str) => self.buffer.push_str(body_str),
388388
None => if contains_comment(&enum_snippet[brace_pos..]) {
389389
self.format_missing_no_indent(span.hi() - BytePos(1))
390-
} else {
391-
self.format_missing(span.hi() - BytePos(1))
392390
},
393391
}
394392
self.block_indent = self.block_indent.block_unindent(self.config);
@@ -535,11 +533,7 @@ pub fn format_impl(
535533
let where_budget = if result.contains('\n') {
536534
context.config.max_width()
537535
} else {
538-
context
539-
.config
540-
.max_width()
541-
.checked_sub(last_line_width(&result))
542-
.unwrap_or(0)
536+
context.budget(last_line_width(&result))
543537
};
544538
let option = WhereClauseOption::snuggled(&ref_and_type);
545539
let where_clause_str = try_opt!(rewrite_where_clause(
@@ -758,11 +752,7 @@ fn format_impl_ref_and_type(
758752
};
759753
let used_space = last_line_width(&result) + trait_ref_overhead + curly_brace_overhead;
760754
// 1 = space before the type.
761-
let budget = context
762-
.config
763-
.max_width()
764-
.checked_sub(used_space + 1)
765-
.unwrap_or(0);
755+
let budget = context.budget(used_space + 1);
766756
if let Some(self_ty_str) = self_ty.rewrite(context, Shape::legacy(budget, offset)) {
767757
if !self_ty_str.contains('\n') {
768758
if trait_ref.is_some() {
@@ -783,7 +773,7 @@ fn format_impl_ref_and_type(
783773
if trait_ref.is_some() {
784774
result.push_str("for ");
785775
}
786-
let budget = context.config.max_width() - last_line_width(&result);
776+
let budget = context.budget(last_line_width(&result));
787777
let type_offset = match context.config.where_style() {
788778
Style::Legacy => new_line_offset + trait_ref_overhead,
789779
Style::Rfc => new_line_offset,
@@ -927,12 +917,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
927917
Density::Tall
928918
};
929919

930-
let where_budget = try_opt!(
931-
context
932-
.config
933-
.max_width()
934-
.checked_sub(last_line_width(&result))
935-
);
920+
let where_budget = context.budget(last_line_width(&result));
936921
let pos_before_where = if type_param_bounds.is_empty() {
937922
generics.where_clause.span.lo()
938923
} else {
@@ -1078,11 +1063,7 @@ pub fn format_struct_struct(
10781063
let overhead = if fields.is_empty() { 3 } else { 2 };
10791064
if (context.config.item_brace_style() == BraceStyle::AlwaysNextLine &&
10801065
!fields.is_empty()) ||
1081-
context
1082-
.config
1083-
.max_width()
1084-
.checked_sub(result.len())
1085-
.unwrap_or(0) < overhead
1066+
context.config.max_width() < overhead + result.len()
10861067
{
10871068
format!("\n{}{{", offset.block_only().to_string(context.config))
10881069
} else {
@@ -1092,12 +1073,10 @@ pub fn format_struct_struct(
10921073
};
10931074
// 1 = `}`
10941075
let overhead = if fields.is_empty() { 1 } else { 0 };
1095-
let max_len = context
1096-
.config
1097-
.max_width()
1098-
.checked_sub(offset.width())
1099-
.unwrap_or(0);
1100-
if !generics_str.contains('\n') && result.len() + generics_str.len() + overhead > max_len {
1076+
let total_width = result.len() + generics_str.len() + overhead;
1077+
if !generics_str.is_empty() && !generics_str.contains('\n') &&
1078+
total_width > context.config.max_width()
1079+
{
11011080
result.push('\n');
11021081
result.push_str(&offset.to_string(context.config));
11031082
result.push_str(&generics_str.trim_left());
@@ -1122,11 +1101,7 @@ pub fn format_struct_struct(
11221101
}
11231102

11241103
// 3 = ` ` and ` }`
1125-
let one_line_budget = context
1126-
.config
1127-
.max_width()
1128-
.checked_sub(result.len() + 3 + offset.width())
1129-
.unwrap_or(0);
1104+
let one_line_budget = context.budget(result.len() + 3 + offset.width());
11301105
let one_line_budget =
11311106
one_line_width.map_or(0, |one_line_width| min(one_line_width, one_line_budget));
11321107

@@ -1286,12 +1261,7 @@ pub fn rewrite_type_alias(
12861261
let generics_str = try_opt!(rewrite_generics(context, generics, shape, g_span));
12871262
result.push_str(&generics_str);
12881263

1289-
let where_budget = try_opt!(
1290-
context
1291-
.config
1292-
.max_width()
1293-
.checked_sub(last_line_width(&result))
1294-
);
1264+
let where_budget = context.budget(last_line_width(&result));
12951265
let option = WhereClauseOption::snuggled(&result);
12961266
let where_clause_str = try_opt!(rewrite_where_clause(
12971267
context,
@@ -1314,11 +1284,7 @@ pub fn rewrite_type_alias(
13141284
let line_width = last_line_width(&result);
13151285
// This checked_sub may fail as the extra space after '=' is not taken into account
13161286
// In that case the budget is set to 0 which will make ty.rewrite retry on a new line
1317-
let budget = context
1318-
.config
1319-
.max_width()
1320-
.checked_sub(indent.width() + line_width + ";".len())
1321-
.unwrap_or(0);
1287+
let budget = context.budget(indent.width() + line_width + ";".len());
13221288
let type_indent = indent + line_width;
13231289
// Try to fit the type on the same line
13241290
let ty_str = try_opt!(
@@ -1331,12 +1297,7 @@ pub fn rewrite_type_alias(
13311297
let type_indent = indent.block_indent(context.config);
13321298
result.push('\n');
13331299
result.push_str(&type_indent.to_string(context.config));
1334-
let budget = try_opt!(
1335-
context
1336-
.config
1337-
.max_width()
1338-
.checked_sub(type_indent.width() + ";".len())
1339-
);
1300+
let budget = context.budget(type_indent.width() + ";".len());
13401301
ty.rewrite(context, Shape::legacy(budget, type_indent))
13411302
})
13421303
);
@@ -1520,7 +1481,7 @@ pub fn rewrite_static(
15201481
if let Some(expr) = expr_opt {
15211482
let lhs = format!("{}{} =", prefix, ty_str);
15221483
// 1 = ;
1523-
let remaining_width = context.config.max_width() - offset.block_indent - 1;
1484+
let remaining_width = context.budget(offset.block_indent + 1);
15241485
rewrite_assign_rhs(
15251486
context,
15261487
lhs,
@@ -1567,7 +1528,7 @@ pub fn rewrite_associated_type(
15671528
let ty_str = try_opt!(ty.rewrite(
15681529
context,
15691530
Shape::legacy(
1570-
context.config.max_width() - indent.block_indent - prefix.len() - 2,
1531+
context.budget(indent.block_indent + prefix.len() + 2),
15711532
indent.block_only(),
15721533
),
15731534
));
@@ -1775,11 +1736,7 @@ fn rewrite_fn_base(
17751736
2
17761737
};
17771738
let used_width = last_line_used_width(&result, indent.width());
1778-
let one_line_budget = context
1779-
.config
1780-
.max_width()
1781-
.checked_sub(used_width + overhead)
1782-
.unwrap_or(0);
1739+
let one_line_budget = context.budget(used_width + overhead);
17831740
let shape = Shape {
17841741
width: one_line_budget,
17851742
indent: indent,
@@ -2015,11 +1972,7 @@ fn rewrite_fn_base(
20151972
};
20161973

20171974
if where_clause.predicates.len() == 1 && should_compress_where {
2018-
let budget = context
2019-
.config
2020-
.max_width()
2021-
.checked_sub(last_line_used_width(&result, indent.width()))
2022-
.unwrap_or(0);
1975+
let budget = context.budget(last_line_used_width(&result, indent.width()));
20231976
if let Some(where_clause_str) = rewrite_where_clause(
20241977
context,
20251978
where_clause,
@@ -2298,27 +2251,19 @@ fn compute_budgets_for_args(
22982251
// 1 = `;`
22992252
used_space += 1;
23002253
}
2301-
let one_line_budget = context
2302-
.config
2303-
.max_width()
2304-
.checked_sub(used_space)
2305-
.unwrap_or(0);
2254+
let one_line_budget = context.budget(used_space);
23062255

23072256
if one_line_budget > 0 {
23082257
// 4 = "() {".len()
23092258
let (indent, multi_line_budget) = match context.config.fn_args_layout() {
23102259
IndentStyle::Block => {
23112260
let indent = indent.block_indent(context.config);
2312-
let budget =
2313-
try_opt!(context.config.max_width().checked_sub(indent.width() + 1));
2314-
(indent, budget)
2261+
(indent, context.budget(indent.width() + 1))
23152262
}
23162263
IndentStyle::Visual => {
23172264
let indent = indent + result.len() + 1;
23182265
let multi_line_overhead = indent.width() + if newline_brace { 2 } else { 4 };
2319-
let budget =
2320-
try_opt!(context.config.max_width().checked_sub(multi_line_overhead));
2321-
(indent, budget)
2266+
(indent, context.budget(multi_line_overhead))
23222267
}
23232268
};
23242269

@@ -2334,8 +2279,7 @@ fn compute_budgets_for_args(
23342279
// Account for `)` and possibly ` {`.
23352280
IndentStyle::Visual => new_indent.width() + if ret_str_len == 0 { 1 } else { 3 },
23362281
};
2337-
let max_space = try_opt!(context.config.max_width().checked_sub(used_space));
2338-
Some((0, max_space, new_indent))
2282+
Some((0, context.budget(used_space), new_indent))
23392283
}
23402284

23412285
fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause, has_body: bool) -> bool {
@@ -2748,11 +2692,7 @@ fn format_generics(
27482692
let mut result = try_opt!(rewrite_generics(context, generics, shape, span));
27492693

27502694
let same_line_brace = if !generics.where_clause.predicates.is_empty() || result.contains('\n') {
2751-
let budget = context
2752-
.config
2753-
.max_width()
2754-
.checked_sub(last_line_used_width(&result, offset.width()))
2755-
.unwrap_or(0);
2695+
let budget = context.budget(last_line_used_width(&result, offset.width()));
27562696
let option = WhereClauseOption::snuggled(&result);
27572697
let where_clause_str = try_opt!(rewrite_where_clause(
27582698
context,
@@ -2773,11 +2713,7 @@ fn format_generics(
27732713
brace_style != BraceStyle::AlwaysNextLine
27742714
};
27752715
let total_used_width = last_line_used_width(&result, used_width);
2776-
let remaining_budget = context
2777-
.config
2778-
.max_width()
2779-
.checked_sub(total_used_width)
2780-
.unwrap_or(0);
2716+
let remaining_budget = context.budget(total_used_width);
27812717
// If the same line brace if forced, it indicates that we are rewriting an item with empty body,
27822718
// and hence we take the closer into account as well for one line budget.
27832719
// We assume that the closer has the same length as the opener.

tests/source/configs-item_brace_style-always_next_line.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// rustfmt-item_brace_style: AlwaysNextLine
22
// Item brace style
33

4+
enum Foo {}
5+
6+
struct Bar {}
7+
48
struct Lorem {
59
ipsum: bool,
610
}

tests/target/configs-item_brace_style-always_next_line.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// rustfmt-item_brace_style: AlwaysNextLine
22
// Item brace style
33

4+
enum Foo {}
5+
6+
struct Bar {}
7+
48
struct Lorem
59
{
610
ipsum: bool,

0 commit comments

Comments
 (0)