Merge pull request #2708 from sinkuu/saturating_sub

Use `saturating_sub` instead of `checked_sub.unwrap_or`
This commit is contained in:
Nick Cameron 2018-05-16 09:02:41 +12:00 committed by GitHub
commit db8cb0b8d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 27 additions and 55 deletions

View File

@ -77,7 +77,7 @@ fn format_derive(context: &RewriteContext, derive_args: &[&str], shape: Shape) -
result.push_str(&(shape.indent + 9).to_string(context.config));
budget = initial_budget;
} else {
budget = budget.checked_sub(width).unwrap_or(0);
budget = budget.saturating_sub(width);
}
result.push_str(a);
if i != num - 1 {

View File

@ -240,10 +240,7 @@ fn rewrite_closure_fn_decl(
);
let item_vec = arg_items.collect::<Vec<_>>();
// 1 = space between arguments and return type.
let horizontal_budget = nested_shape
.width
.checked_sub(ret_str.len() + 1)
.unwrap_or(0);
let horizontal_budget = nested_shape.width.saturating_sub(ret_str.len() + 1);
let tactic = definitive_tactic(
&item_vec,
ListTactic::HorizontalVertical,

View File

@ -464,7 +464,7 @@ fn rewrite_comment_inner(
// 1 = " "
let offset = 1 + last_line_width(&result) - line_start.len();
Shape {
width: max_chars.checked_sub(offset).unwrap_or(0),
width: max_chars.saturating_sub(offset),
indent: fmt_indent,
offset: fmt.shape.offset + offset,
}

View File

@ -326,7 +326,7 @@ pub fn format_expr(
rw
} else {
// 9 = `do catch `
let budget = shape.width.checked_sub(9).unwrap_or(0);
let budget = shape.width.saturating_sub(9);
Some(format!(
"{}{}",
"do catch ",
@ -1002,8 +1002,7 @@ impl<'a> ControlFlow<'a> {
let one_line_budget = context
.config
.max_width()
.checked_sub(constr_shape.used_width() + offset + brace_overhead)
.unwrap_or(0);
.saturating_sub(constr_shape.used_width() + offset + brace_overhead);
let force_newline_brace = (pat_expr_string.contains('\n')
|| pat_expr_string.len() > one_line_budget)
&& !last_line_extendable(&pat_expr_string);
@ -1109,7 +1108,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
return Some(cond_str);
}
let block_width = shape.width.checked_sub(used_width).unwrap_or(0);
let block_width = shape.width.saturating_sub(used_width);
// This is used only for the empty block case: `{}`. So, we use 1 if we know
// we should avoid the single line case.
let block_width = if self.else_block.is_some() || self.nested_if {
@ -1828,7 +1827,7 @@ pub fn rewrite_field(
Some(attrs_str + &name)
} else {
let mut separator = String::from(struct_lit_field_separator(context.config));
for _ in 0..prefix_max_width.checked_sub(name.len()).unwrap_or(0) {
for _ in 0..prefix_max_width.saturating_sub(name.len()) {
separator.push(' ');
}
let overhead = name.len() + separator.len();
@ -2053,13 +2052,11 @@ pub fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
rhs_tactics: RhsTactics,
) -> Option<String> {
let lhs = lhs.into();
let last_line_width = last_line_width(&lhs)
.checked_sub(if lhs.contains('\n') {
shape.indent.width()
} else {
0
})
.unwrap_or(0);
let last_line_width = last_line_width(&lhs).saturating_sub(if lhs.contains('\n') {
shape.indent.width()
} else {
0
});
// 1 = space between operator and rhs.
let orig_shape = shape.offset_left(last_line_width + 1).unwrap_or(Shape {
width: 0,

View File

@ -703,7 +703,7 @@ fn rewrite_nested_use_tree(
let remaining_width = if has_nested_list {
0
} else {
shape.width.checked_sub(2).unwrap_or(0)
shape.width.saturating_sub(2)
};
let tactic = definitive_tactic(

View File

@ -1503,7 +1503,7 @@ pub fn rewrite_struct_field(
attrs_extendable,
)?;
let overhead = last_line_width(&attr_prefix);
let lhs_offset = lhs_max_width.checked_sub(overhead).unwrap_or(0);
let lhs_offset = lhs_max_width.saturating_sub(overhead);
for _ in 0..lhs_offset {
spacing.push(' ');
}

View File

@ -193,7 +193,7 @@ where
};
let (sep_count, total_width) = calculate_width(items.clone());
let total_sep_len = sep.len() * sep_count.checked_sub(1).unwrap_or(0);
let total_sep_len = sep.len() * sep_count.saturating_sub(1);
let real_total = total_width + total_sep_len;
if real_total <= limit
@ -485,9 +485,7 @@ where
}
fn post_comment_alignment(item_max_width: Option<usize>, inner_item_len: usize) -> usize {
item_max_width
.and_then(|max_line_width| max_line_width.checked_sub(inner_item_len))
.unwrap_or(0)
item_max_width.unwrap_or(0).saturating_sub(inner_item_len)
}
pub struct ListItems<'a, I, F1, F2, F3>

View File

@ -1091,9 +1091,7 @@ fn indent_macro_snippet(
_ if !trimmed => line.to_owned(),
Some(original_indent_width) => {
let new_indent_width = indent.width()
+ original_indent_width
.checked_sub(min_prefix_space_width)
.unwrap_or(0);
+ original_indent_width.saturating_sub(min_prefix_space_width);
let new_indent = Indent::from_width(context.config, new_indent_width);
format!("{}{}", new_indent.to_string(context.config), line.trim())
}

View File

@ -196,7 +196,7 @@ fn rewrite_match_arms(
let arm_len = arms.len();
let is_last_iter = repeat(false)
.take(arm_len.checked_sub(1).unwrap_or(0))
.take(arm_len.saturating_sub(1))
.chain(repeat(true));
let beginning_verts = collect_beginning_verts(context, arms, span);
let items = itemize_list(

View File

@ -147,10 +147,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
1
};
let used_width = extra_offset(ident, shape);
let one_line_width = shape
.width
.checked_sub(used_width + 2 * paren_overhead)
.unwrap_or(0);
let one_line_width = shape.width.saturating_sub(used_width + 2 * paren_overhead);
// 1 = "(" or ")"
let one_line_shape = shape
@ -412,10 +409,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
fn wrap_items(&self, items_str: &str, shape: Shape, is_extendable: bool) -> String {
let shape = Shape {
width: shape
.width
.checked_sub(last_line_width(self.ident))
.unwrap_or(0),
width: shape.width.saturating_sub(last_line_width(self.ident)),
..shape
};

View File

@ -53,7 +53,7 @@ impl<'a> RewriteContext<'a> {
}
pub fn budget(&self, used_width: usize) -> usize {
self.config.max_width().checked_sub(used_width).unwrap_or(0)
self.config.max_width().saturating_sub(used_width)
}
pub fn inside_macro(&self) -> bool {

View File

@ -181,7 +181,7 @@ impl Shape {
pub fn indented(indent: Indent, config: &Config) -> Shape {
Shape {
width: config.max_width().checked_sub(indent.width()).unwrap_or(0),
width: config.max_width().saturating_sub(indent.width()),
indent,
offset: indent.alignment,
}
@ -189,10 +189,7 @@ impl Shape {
pub fn with_max_width(&self, config: &Config) -> Shape {
Shape {
width: config
.max_width()
.checked_sub(self.indent.width())
.unwrap_or(0),
width: config.max_width().saturating_sub(self.indent.width()),
..*self
}
}
@ -266,17 +263,13 @@ impl Shape {
pub fn rhs_overhead(&self, config: &Config) -> usize {
config
.max_width()
.checked_sub(self.used_width() + self.width)
.unwrap_or(0)
.saturating_sub(self.used_width() + self.width)
}
pub fn comment(&self, config: &Config) -> Shape {
let width = min(
self.width,
config
.comment_width()
.checked_sub(self.indent.width())
.unwrap_or(0),
config.comment_width().saturating_sub(self.indent.width()),
);
Shape { width, ..*self }
}

View File

@ -29,10 +29,7 @@ pub const SKIP_ANNOTATION: &str = "rustfmt::skip";
pub fn extra_offset(text: &str, shape: Shape) -> usize {
match text.rfind('\n') {
// 1 for newline character
Some(idx) => text
.len()
.checked_sub(idx + 1 + shape.used_width())
.unwrap_or(0),
Some(idx) => text.len().saturating_sub(idx + 1 + shape.used_width()),
None => text.len(),
}
}

View File

@ -219,9 +219,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
let item_shape = Shape::indented(item_indent, context.config).sub_width(1)?;
let (mut field_prefix_max_width, field_prefix_min_width) =
struct_field_prefix_max_min_width(context, fields, item_shape);
let max_diff = field_prefix_max_width
.checked_sub(field_prefix_min_width)
.unwrap_or(0);
let max_diff = field_prefix_max_width.saturating_sub(field_prefix_min_width);
if max_diff > context.config.struct_field_align_threshold() {
field_prefix_max_width = 0;
}