Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6338782

Browse files
committed
Avoid using to_expr as much as possible
1 parent c302409 commit 6338782

File tree

3 files changed

+54
-38
lines changed

3 files changed

+54
-38
lines changed

src/closures.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,10 @@ pub fn rewrite_last_closure(
361361
/// Returns true if the given vector of arguments has more than one `ast::ExprKind::Closure`.
362362
pub fn args_have_many_closure(args: &[OverflowableItem]) -> bool {
363363
args.iter()
364-
.filter(|arg| {
365-
arg.to_expr()
366-
.map(|e| match e.node {
367-
ast::ExprKind::Closure(..) => true,
368-
_ => false,
369-
})
370-
.unwrap_or(false)
364+
.filter_map(|arg| arg.to_expr())
365+
.filter(|expr| match expr.node {
366+
ast::ExprKind::Closure(..) => true,
367+
_ => false,
371368
})
372369
.count()
373370
> 1

src/expr.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,7 @@ pub fn rewrite_call(
13371337
)
13381338
}
13391339

1340-
fn is_simple_expr(expr: &ast::Expr) -> bool {
1340+
pub fn is_simple_expr(expr: &ast::Expr) -> bool {
13411341
match expr.node {
13421342
ast::ExprKind::Lit(..) => true,
13431343
ast::ExprKind::Path(ref qself, ref path) => qself.is_none() && path.segments.len() <= 1,
@@ -1356,9 +1356,7 @@ fn is_simple_expr(expr: &ast::Expr) -> bool {
13561356
}
13571357

13581358
pub fn is_every_expr_simple(lists: &[OverflowableItem]) -> bool {
1359-
lists
1360-
.iter()
1361-
.all(|arg| arg.to_expr().map_or(false, is_simple_expr))
1359+
lists.iter().all(OverflowableItem::is_simple)
13621360
}
13631361

13641362
pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_len: usize) -> bool {

src/overflow.rs

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use syntax::{ast, ptr};
1717

1818
use closures;
1919
use expr::{
20-
can_be_overflowed_expr, is_every_expr_simple, is_method_call, is_nested_call,
20+
can_be_overflowed_expr, is_every_expr_simple, is_method_call, is_nested_call, is_simple_expr,
2121
maybe_get_args_offset,
2222
};
2323
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
@@ -70,6 +70,30 @@ impl<'a> OverflowableItem<'a> {
7070
}
7171
}
7272

73+
pub fn is_simple(&self) -> bool {
74+
match self {
75+
OverflowableItem::Expr(expr) => is_simple_expr(expr),
76+
OverflowableItem::MacroArg(MacroArg::Expr(expr)) => is_simple_expr(expr),
77+
_ => false,
78+
}
79+
}
80+
81+
pub fn is_expr(&self) -> bool {
82+
match self {
83+
OverflowableItem::Expr(..) => true,
84+
OverflowableItem::MacroArg(MacroArg::Expr(..)) => true,
85+
_ => false,
86+
}
87+
}
88+
89+
pub fn is_nested_call(&self) -> bool {
90+
match self {
91+
OverflowableItem::Expr(expr) => is_nested_call(expr),
92+
OverflowableItem::MacroArg(MacroArg::Expr(expr)) => is_nested_call(expr),
93+
_ => false,
94+
}
95+
}
96+
7397
pub fn to_expr(&self) -> Option<&'a ast::Expr> {
7498
match self {
7599
OverflowableItem::Expr(expr) => Some(expr),
@@ -303,23 +327,24 @@ impl<'a> Context<'a> {
303327
shape: Shape,
304328
) -> Option<String> {
305329
let last_item = self.last_item()?;
306-
let rewrite = if let Some(expr) = last_item.to_expr() {
307-
match expr.node {
308-
// When overflowing the closure which consists of a single control flow expression,
309-
// force to use block if its condition uses multi line.
310-
ast::ExprKind::Closure(..) => {
311-
// If the argument consists of multiple closures, we do not overflow
312-
// the last closure.
313-
if closures::args_have_many_closure(&self.items) {
314-
None
315-
} else {
316-
closures::rewrite_last_closure(self.context, expr, shape)
330+
let rewrite = match last_item {
331+
OverflowableItem::Expr(ref expr) => {
332+
match expr.node {
333+
// When overflowing the closure which consists of a single control flow
334+
// expression, force to use block if its condition uses multi line.
335+
ast::ExprKind::Closure(..) => {
336+
// If the argument consists of multiple closures, we do not overflow
337+
// the last closure.
338+
if closures::args_have_many_closure(&self.items) {
339+
None
340+
} else {
341+
closures::rewrite_last_closure(self.context, expr, shape)
342+
}
317343
}
344+
_ => expr.rewrite(self.context, shape),
318345
}
319-
_ => expr.rewrite(self.context, shape),
320346
}
321-
} else {
322-
last_item.rewrite(self.context, shape)
347+
item @ _ => item.rewrite(self.context, shape),
323348
};
324349

325350
if let Some(rewrite) = rewrite {
@@ -343,20 +368,21 @@ impl<'a> Context<'a> {
343368
fn try_overflow_last_item(&self, list_items: &mut Vec<ListItem>) -> DefinitiveListTactic {
344369
// 1 = "("
345370
let combine_arg_with_callee = self.items.len() == 1
346-
&& self.items[0].to_expr().is_some()
371+
&& self.items[0].is_expr()
347372
&& self.ident.len() < self.context.config.tab_spaces();
348373
let overflow_last = combine_arg_with_callee || can_be_overflowed(self.context, &self.items);
349374

350375
// Replace the last item with its first line to see if it fits with
351376
// first arguments.
352377
let placeholder = if overflow_last {
353378
let old_value = *self.context.force_one_line_chain.borrow();
354-
if !combine_arg_with_callee {
355-
if let Some(ref expr) = self.last_item().and_then(|item| item.to_expr()) {
356-
if is_method_call(expr) {
357-
self.context.force_one_line_chain.replace(true);
358-
}
379+
match self.last_item() {
380+
Some(OverflowableItem::Expr(expr))
381+
if !combine_arg_with_callee && is_method_call(expr) =>
382+
{
383+
self.context.force_one_line_chain.replace(true);
359384
}
385+
_ => (),
360386
}
361387
let result = last_item_shape(
362388
&self.items,
@@ -596,12 +622,7 @@ fn last_item_shape(
596622
shape: Shape,
597623
args_max_width: usize,
598624
) -> Option<Shape> {
599-
let is_nested_call = lists
600-
.iter()
601-
.next()
602-
.and_then(|item| item.to_expr())
603-
.map_or(false, is_nested_call);
604-
if items.len() == 1 && !is_nested_call {
625+
if items.len() == 1 && !lists.iter().next()?.is_nested_call() {
605626
return Some(shape);
606627
}
607628
let offset = items.iter().rev().skip(1).fold(0, |acc, i| {

0 commit comments

Comments
 (0)