From 436a083fceae8e0f71605aa07dce047a316576c6 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 28 Sep 2017 16:33:30 +0900 Subject: [PATCH] Change return type of rewrite_call_inner() to Option --- src/expr.rs | 27 +++++++++++---------------- src/items.rs | 20 +++++++++----------- src/macros.rs | 5 ++--- src/patterns.rs | 2 +- 4 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index dc6504de0b11..4aeeec4083bc 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::cmp::{min, Ordering}; +use std::cmp::min; use std::borrow::Cow; use std::fmt::Write; use std::iter::{repeat, ExactSizeIterator}; @@ -2033,7 +2033,7 @@ pub fn rewrite_call( shape, context.config.fn_call_width(), force_trailing_comma, - ).ok() + ) } pub fn rewrite_call_inner<'a, T>( @@ -2044,7 +2044,7 @@ pub fn rewrite_call_inner<'a, T>( shape: Shape, args_max_width: usize, force_trailing_comma: bool, -) -> Result +) -> Option where T: Rewrite + Spanned + ToExpr + 'a, { @@ -2055,22 +2055,19 @@ where 1 }; let used_width = extra_offset(callee_str, shape); - let one_line_width = shape - .width - .checked_sub(used_width + 2 * paren_overhead) - .ok_or(Ordering::Greater)?; + let one_line_width = try_opt!(shape.width.checked_sub(used_width + 2 * paren_overhead)); - let nested_shape = shape_from_fn_call_style( + let nested_shape = try_opt!(shape_from_fn_call_style( context, shape, used_width + 2 * paren_overhead, used_width + paren_overhead, - ).ok_or(Ordering::Greater)?; + )); let span_lo = context.codemap.span_after(span, "("); let args_span = mk_sp(span_lo, span.hi()); - let (extendable, list_str) = rewrite_call_args( + let (extendable, list_str) = try_opt!(rewrite_call_args( context, args, args_span, @@ -2078,7 +2075,7 @@ where one_line_width, args_max_width, force_trailing_comma, - ).ok_or(Ordering::Less)?; + )); if !context.use_block_indent() && need_block_indent(&list_str, nested_shape) && !extendable { let mut new_context = context.clone(); @@ -2094,10 +2091,8 @@ where ); } - let args_shape = shape - .sub_width(last_line_width(callee_str)) - .ok_or(Ordering::Less)?; - Ok(format!( + let args_shape = try_opt!(shape.sub_width(last_line_width(callee_str))); + Some(format!( "{}{}", callee_str, wrap_args_with_parens(context, &list_str, extendable, args_shape, nested_shape) @@ -2805,7 +2800,7 @@ where shape, context.config.fn_call_width(), force_trailing_comma, - ).ok() + ) } else { rewrite_tuple_in_visual_indent_style(context, items, span, shape) } diff --git a/src/items.rs b/src/items.rs index 9d35c83501f0..c546c2ca94b9 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1211,17 +1211,15 @@ fn format_tuple_struct( result.push(')'); } else { // 3 = `();` - let body = try_opt!( - rewrite_call_inner( - context, - "", - &fields.iter().map(|field| field).collect::>()[..], - span, - Shape::legacy(context.budget(last_line_width(&result) + 3), offset), - context.config.fn_call_width(), - false, - ).ok() - ); + let body = try_opt!(rewrite_call_inner( + context, + "", + &fields.iter().map(|field| field).collect::>()[..], + span, + Shape::legacy(context.budget(last_line_width(&result) + 3), offset), + context.config.fn_call_width(), + false, + )); result.push_str(&body); } diff --git a/src/macros.rs b/src/macros.rs index 5b52567087ec..4eaf97dd254c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -202,7 +202,7 @@ pub fn rewrite_macro( MacroStyle::Parens => { // Format macro invocation as function call, forcing no trailing // comma because not all macros support them. - let rw = rewrite_call_inner( + rewrite_call_inner( context, ¯o_name, &arg_vec.iter().map(|e| &*e).collect::>()[..], @@ -210,8 +210,7 @@ pub fn rewrite_macro( shape, context.config.fn_call_width(), trailing_comma, - ); - rw.ok().map(|rw| match position { + ).map(|rw| match position { MacroPosition::Item => format!("{};", rw), _ => rw, }) diff --git a/src/patterns.rs b/src/patterns.rs index c58a98627ff5..7d2f81dc229d 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -351,7 +351,7 @@ fn rewrite_tuple_pat( shape, shape.width, add_comma, - ).ok() + ) } fn count_wildcard_suffix_len(