Skip to content

Commit 392d34b

Browse files
authored
Merge pull request #1711 from topecongiro/combine-control-flow-condition-body
Combine control flow condition and body
2 parents 4368cd3 + 91bd1fd commit 392d34b

File tree

6 files changed

+21
-23
lines changed

6 files changed

+21
-23
lines changed

src/chains.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
7979
use Shape;
8080
use rewrite::{Rewrite, RewriteContext};
81-
use utils::{wrap_str, first_line_width, last_line_width, mk_sp};
81+
use utils::{wrap_str, first_line_width, last_line_width, mk_sp, last_line_extendable};
8282
use expr::rewrite_call;
8383
use config::IndentStyle;
8484
use macros::convert_try_mac;
@@ -226,8 +226,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
226226
total_span,
227227
context,
228228
shape,
229-
)
230-
{
229+
) {
231230
// If the first line of the last method does not fit into a single line
232231
// after the others, allow new lines.
233232
almost_total + first_line_width(&last[0]) < context.config.max_width()
@@ -322,12 +321,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
322321
}
323322

324323
fn is_extendable_parent(context: &RewriteContext, parent_str: &str) -> bool {
325-
context.config.chain_indent() == IndentStyle::Block &&
326-
parent_str.lines().last().map_or(false, |s| {
327-
s.trim()
328-
.chars()
329-
.all(|c| c == ')' || c == ']' || c == '}' || c == '?')
330-
})
324+
context.config.chain_indent() == IndentStyle::Block && last_line_extendable(parent_str)
331325
}
332326

333327
// True if the chain is only `?`s.

src/expr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTacti
2121
use string::{StringFormat, rewrite_string};
2222
use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
2323
semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr,
24-
colon_spaces, contains_skip, mk_sp};
24+
colon_spaces, contains_skip, mk_sp, last_line_extendable};
2525
use visitor::FmtVisitor;
2626
use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style};
2727
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
@@ -1145,7 +1145,8 @@ impl<'a> ControlFlow<'a> {
11451145
};
11461146

11471147
let force_newline_brace = context.config.control_style() == Style::Rfc &&
1148-
pat_expr_string.contains('\n');
1148+
pat_expr_string.contains('\n') &&
1149+
!last_line_extendable(&pat_expr_string);
11491150

11501151
// Try to format if-else on single line.
11511152
if self.allow_single_line && context.config.single_line_if_else_max_width() > 0 {

src/items.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,7 @@ pub fn format_impl(
588588
&result,
589589
&where_clause_str,
590590
&item,
591-
))
592-
{
591+
)) {
593592
result.push_str(&where_clause_str);
594593
if where_clause_str.contains('\n') {
595594
let white_space = offset.to_string(context.config);
@@ -736,8 +735,7 @@ fn format_impl_ref_and_type(
736735
true,
737736
polarity_str,
738737
result_len,
739-
)
740-
{
738+
) {
741739
result.push_str(&trait_ref_str);
742740
} else {
743741
let generics_str = try_opt!(rewrite_generics_inner(
@@ -2114,8 +2112,7 @@ fn rewrite_fn_base(
21142112
!has_braces,
21152113
put_args_in_block && ret_str.is_empty(),
21162114
Some(span.hi),
2117-
)
2118-
{
2115+
) {
21192116
if !where_clause_str.contains('\n') {
21202117
if last_line_width(&result) + where_clause_str.len() > context.config.max_width() {
21212118
result.push('\n');

src/patterns.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ fn count_wildcard_suffix_len(
376376
for item in items.iter().rev().take_while(|i| match i.item {
377377
Some(ref internal_string) if internal_string == "_" => true,
378378
_ => false,
379-
})
380-
{
379+
}) {
381380
suffix_len += 1;
382381

383382
if item.pre_comment.is_some() || item.post_comment.is_some() {

src/utils.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ pub fn trimmed_last_line_width(s: &str) -> usize {
108108
}
109109
}
110110

111+
#[inline]
112+
pub fn last_line_extendable(s: &str) -> bool {
113+
s.lines().last().map_or(false, |s| {
114+
s.trim()
115+
.chars()
116+
.all(|c| c == ')' || c == ']' || c == '}' || c == '?')
117+
})
118+
}
119+
111120
#[inline]
112121
fn is_skip(meta_item: &MetaItem) -> bool {
113122
match meta_item.node {

src/visitor.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,7 @@ impl<'a> FmtVisitor<'a> {
298298
item,
299299
self.block_indent,
300300
where_span_end,
301-
)
302-
{
301+
) {
303302
self.buffer.push_str(&impl_str);
304303
self.last_pos = source!(self, item.span).hi;
305304
}
@@ -310,8 +309,7 @@ impl<'a> FmtVisitor<'a> {
310309
&self.get_context(),
311310
item,
312311
self.block_indent,
313-
)
314-
{
312+
) {
315313
self.buffer.push_str(&trait_str);
316314
self.last_pos = source!(self, item.span).hi;
317315
}

0 commit comments

Comments
 (0)