about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorShotaro Yamada <sinkuu@sinkuu.xyz>2018-05-14 21:58:57 +0900
committerShotaro Yamada <sinkuu@sinkuu.xyz>2018-05-15 20:41:43 +0900
commitd1e2b80fb9d3e48a63f7c3c97fc966869da60a2c (patch)
tree628262ab09f44f3399c7e9e9fa85d0d09cf05105 /src
parent45c0b47e6daedcca449cc1e82592e0329fa7d8c0 (diff)
Use saturating_sub
Diffstat (limited to 'src')
-rw-r--r--src/attr.rs2
-rw-r--r--src/closures.rs5
-rw-r--r--src/comment.rs2
-rw-r--r--src/expr.rs21
-rw-r--r--src/imports.rs2
-rw-r--r--src/items.rs2
-rw-r--r--src/lists.rs6
-rw-r--r--src/macros.rs4
-rw-r--r--src/matches.rs2
-rw-r--r--src/overflow.rs10
-rw-r--r--src/rewrite.rs2
-rw-r--r--src/shape.rs15
-rw-r--r--src/utils.rs5
-rw-r--r--src/vertical.rs4
14 files changed, 27 insertions, 55 deletions
diff --git a/src/attr.rs b/src/attr.rs
index e754144a727..28e3e24ea09 100644
--- a/src/attr.rs
+++ b/src/attr.rs
@@ -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 {
diff --git a/src/closures.rs b/src/closures.rs
index 0d5cf63a197..aec7d34b60a 100644
--- a/src/closures.rs
+++ b/src/closures.rs
@@ -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,
diff --git a/src/comment.rs b/src/comment.rs
index f850c0bba44..044a76e3599 100644
--- a/src/comment.rs
+++ b/src/comment.rs
@@ -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,
                 }
diff --git a/src/expr.rs b/src/expr.rs
index 429fbc3cf9a..12605af25ae 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -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,
diff --git a/src/imports.rs b/src/imports.rs
index 83121b04213..4740921c525 100644
--- a/src/imports.rs
+++ b/src/imports.rs
@@ -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(
diff --git a/src/items.rs b/src/items.rs
index bd1a83d5a66..8c90d73cff3 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -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(' ');
     }
diff --git a/src/lists.rs b/src/lists.rs
index b3264487cfe..7d612913361 100644
--- a/src/lists.rs
+++ b/src/lists.rs
@@ -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>
diff --git a/src/macros.rs b/src/macros.rs
index 4c1b0baff12..7205c39da48 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -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())
                         }
diff --git a/src/matches.rs b/src/matches.rs
index 0bbdbe62017..5a6aa6c6aac 100644
--- a/src/matches.rs
+++ b/src/matches.rs
@@ -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(
diff --git a/src/overflow.rs b/src/overflow.rs
index a8ef66b0e3c..aeb528bf2e0 100644
--- a/src/overflow.rs
+++ b/src/overflow.rs
@@ -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
         };
 
diff --git a/src/rewrite.rs b/src/rewrite.rs
index 7c7bb060fd3..0dfae84bfc2 100644
--- a/src/rewrite.rs
+++ b/src/rewrite.rs
@@ -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 {
diff --git a/src/shape.rs b/src/shape.rs
index 8a17389672f..6cf0e8ea890 100644
--- a/src/shape.rs
+++ b/src/shape.rs
@@ -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 }
     }
diff --git a/src/utils.rs b/src/utils.rs
index 991ebf16bf2..6c8862b9bf5 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -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(),
     }
 }
diff --git a/src/vertical.rs b/src/vertical.rs
index 6cdaaafeb36..1595b22c5c4 100644
--- a/src/vertical.rs
+++ b/src/vertical.rs
@@ -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;
     }