mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-02 11:44:28 +00:00
Merge pull request #2708 from sinkuu/saturating_sub
Use `saturating_sub` instead of `checked_sub.unwrap_or`
This commit is contained in:
commit
db8cb0b8d6
@ -77,7 +77,7 @@ fn format_derive(context: &RewriteContext, derive_args: &[&str], shape: Shape) -
|
|||||||
result.push_str(&(shape.indent + 9).to_string(context.config));
|
result.push_str(&(shape.indent + 9).to_string(context.config));
|
||||||
budget = initial_budget;
|
budget = initial_budget;
|
||||||
} else {
|
} else {
|
||||||
budget = budget.checked_sub(width).unwrap_or(0);
|
budget = budget.saturating_sub(width);
|
||||||
}
|
}
|
||||||
result.push_str(a);
|
result.push_str(a);
|
||||||
if i != num - 1 {
|
if i != num - 1 {
|
||||||
|
@ -240,10 +240,7 @@ fn rewrite_closure_fn_decl(
|
|||||||
);
|
);
|
||||||
let item_vec = arg_items.collect::<Vec<_>>();
|
let item_vec = arg_items.collect::<Vec<_>>();
|
||||||
// 1 = space between arguments and return type.
|
// 1 = space between arguments and return type.
|
||||||
let horizontal_budget = nested_shape
|
let horizontal_budget = nested_shape.width.saturating_sub(ret_str.len() + 1);
|
||||||
.width
|
|
||||||
.checked_sub(ret_str.len() + 1)
|
|
||||||
.unwrap_or(0);
|
|
||||||
let tactic = definitive_tactic(
|
let tactic = definitive_tactic(
|
||||||
&item_vec,
|
&item_vec,
|
||||||
ListTactic::HorizontalVertical,
|
ListTactic::HorizontalVertical,
|
||||||
|
@ -464,7 +464,7 @@ fn rewrite_comment_inner(
|
|||||||
// 1 = " "
|
// 1 = " "
|
||||||
let offset = 1 + last_line_width(&result) - line_start.len();
|
let offset = 1 + last_line_width(&result) - line_start.len();
|
||||||
Shape {
|
Shape {
|
||||||
width: max_chars.checked_sub(offset).unwrap_or(0),
|
width: max_chars.saturating_sub(offset),
|
||||||
indent: fmt_indent,
|
indent: fmt_indent,
|
||||||
offset: fmt.shape.offset + offset,
|
offset: fmt.shape.offset + offset,
|
||||||
}
|
}
|
||||||
|
21
src/expr.rs
21
src/expr.rs
@ -326,7 +326,7 @@ pub fn format_expr(
|
|||||||
rw
|
rw
|
||||||
} else {
|
} else {
|
||||||
// 9 = `do catch `
|
// 9 = `do catch `
|
||||||
let budget = shape.width.checked_sub(9).unwrap_or(0);
|
let budget = shape.width.saturating_sub(9);
|
||||||
Some(format!(
|
Some(format!(
|
||||||
"{}{}",
|
"{}{}",
|
||||||
"do catch ",
|
"do catch ",
|
||||||
@ -1002,8 +1002,7 @@ impl<'a> ControlFlow<'a> {
|
|||||||
let one_line_budget = context
|
let one_line_budget = context
|
||||||
.config
|
.config
|
||||||
.max_width()
|
.max_width()
|
||||||
.checked_sub(constr_shape.used_width() + offset + brace_overhead)
|
.saturating_sub(constr_shape.used_width() + offset + brace_overhead);
|
||||||
.unwrap_or(0);
|
|
||||||
let force_newline_brace = (pat_expr_string.contains('\n')
|
let force_newline_brace = (pat_expr_string.contains('\n')
|
||||||
|| pat_expr_string.len() > one_line_budget)
|
|| pat_expr_string.len() > one_line_budget)
|
||||||
&& !last_line_extendable(&pat_expr_string);
|
&& !last_line_extendable(&pat_expr_string);
|
||||||
@ -1109,7 +1108,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
|
|||||||
return Some(cond_str);
|
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
|
// This is used only for the empty block case: `{}`. So, we use 1 if we know
|
||||||
// we should avoid the single line case.
|
// we should avoid the single line case.
|
||||||
let block_width = if self.else_block.is_some() || self.nested_if {
|
let block_width = if self.else_block.is_some() || self.nested_if {
|
||||||
@ -1828,7 +1827,7 @@ pub fn rewrite_field(
|
|||||||
Some(attrs_str + &name)
|
Some(attrs_str + &name)
|
||||||
} else {
|
} else {
|
||||||
let mut separator = String::from(struct_lit_field_separator(context.config));
|
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(' ');
|
separator.push(' ');
|
||||||
}
|
}
|
||||||
let overhead = name.len() + separator.len();
|
let overhead = name.len() + separator.len();
|
||||||
@ -2053,13 +2052,11 @@ pub fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
|
|||||||
rhs_tactics: RhsTactics,
|
rhs_tactics: RhsTactics,
|
||||||
) -> Option<String> {
|
) -> Option<String> {
|
||||||
let lhs = lhs.into();
|
let lhs = lhs.into();
|
||||||
let last_line_width = last_line_width(&lhs)
|
let last_line_width = last_line_width(&lhs).saturating_sub(if lhs.contains('\n') {
|
||||||
.checked_sub(if lhs.contains('\n') {
|
shape.indent.width()
|
||||||
shape.indent.width()
|
} else {
|
||||||
} else {
|
0
|
||||||
0
|
});
|
||||||
})
|
|
||||||
.unwrap_or(0);
|
|
||||||
// 1 = space between operator and rhs.
|
// 1 = space between operator and rhs.
|
||||||
let orig_shape = shape.offset_left(last_line_width + 1).unwrap_or(Shape {
|
let orig_shape = shape.offset_left(last_line_width + 1).unwrap_or(Shape {
|
||||||
width: 0,
|
width: 0,
|
||||||
|
@ -703,7 +703,7 @@ fn rewrite_nested_use_tree(
|
|||||||
let remaining_width = if has_nested_list {
|
let remaining_width = if has_nested_list {
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
shape.width.checked_sub(2).unwrap_or(0)
|
shape.width.saturating_sub(2)
|
||||||
};
|
};
|
||||||
|
|
||||||
let tactic = definitive_tactic(
|
let tactic = definitive_tactic(
|
||||||
|
@ -1503,7 +1503,7 @@ pub fn rewrite_struct_field(
|
|||||||
attrs_extendable,
|
attrs_extendable,
|
||||||
)?;
|
)?;
|
||||||
let overhead = last_line_width(&attr_prefix);
|
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 {
|
for _ in 0..lhs_offset {
|
||||||
spacing.push(' ');
|
spacing.push(' ');
|
||||||
}
|
}
|
||||||
|
@ -193,7 +193,7 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
let (sep_count, total_width) = calculate_width(items.clone());
|
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;
|
let real_total = total_width + total_sep_len;
|
||||||
|
|
||||||
if real_total <= limit
|
if real_total <= limit
|
||||||
@ -485,9 +485,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn post_comment_alignment(item_max_width: Option<usize>, inner_item_len: usize) -> usize {
|
fn post_comment_alignment(item_max_width: Option<usize>, inner_item_len: usize) -> usize {
|
||||||
item_max_width
|
item_max_width.unwrap_or(0).saturating_sub(inner_item_len)
|
||||||
.and_then(|max_line_width| max_line_width.checked_sub(inner_item_len))
|
|
||||||
.unwrap_or(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ListItems<'a, I, F1, F2, F3>
|
pub struct ListItems<'a, I, F1, F2, F3>
|
||||||
|
@ -1091,9 +1091,7 @@ fn indent_macro_snippet(
|
|||||||
_ if !trimmed => line.to_owned(),
|
_ if !trimmed => line.to_owned(),
|
||||||
Some(original_indent_width) => {
|
Some(original_indent_width) => {
|
||||||
let new_indent_width = indent.width()
|
let new_indent_width = indent.width()
|
||||||
+ original_indent_width
|
+ original_indent_width.saturating_sub(min_prefix_space_width);
|
||||||
.checked_sub(min_prefix_space_width)
|
|
||||||
.unwrap_or(0);
|
|
||||||
let new_indent = Indent::from_width(context.config, new_indent_width);
|
let new_indent = Indent::from_width(context.config, new_indent_width);
|
||||||
format!("{}{}", new_indent.to_string(context.config), line.trim())
|
format!("{}{}", new_indent.to_string(context.config), line.trim())
|
||||||
}
|
}
|
||||||
|
@ -196,7 +196,7 @@ fn rewrite_match_arms(
|
|||||||
|
|
||||||
let arm_len = arms.len();
|
let arm_len = arms.len();
|
||||||
let is_last_iter = repeat(false)
|
let is_last_iter = repeat(false)
|
||||||
.take(arm_len.checked_sub(1).unwrap_or(0))
|
.take(arm_len.saturating_sub(1))
|
||||||
.chain(repeat(true));
|
.chain(repeat(true));
|
||||||
let beginning_verts = collect_beginning_verts(context, arms, span);
|
let beginning_verts = collect_beginning_verts(context, arms, span);
|
||||||
let items = itemize_list(
|
let items = itemize_list(
|
||||||
|
@ -147,10 +147,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
|
|||||||
1
|
1
|
||||||
};
|
};
|
||||||
let used_width = extra_offset(ident, shape);
|
let used_width = extra_offset(ident, shape);
|
||||||
let one_line_width = shape
|
let one_line_width = shape.width.saturating_sub(used_width + 2 * paren_overhead);
|
||||||
.width
|
|
||||||
.checked_sub(used_width + 2 * paren_overhead)
|
|
||||||
.unwrap_or(0);
|
|
||||||
|
|
||||||
// 1 = "(" or ")"
|
// 1 = "(" or ")"
|
||||||
let one_line_shape = shape
|
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 {
|
fn wrap_items(&self, items_str: &str, shape: Shape, is_extendable: bool) -> String {
|
||||||
let shape = Shape {
|
let shape = Shape {
|
||||||
width: shape
|
width: shape.width.saturating_sub(last_line_width(self.ident)),
|
||||||
.width
|
|
||||||
.checked_sub(last_line_width(self.ident))
|
|
||||||
.unwrap_or(0),
|
|
||||||
..shape
|
..shape
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ impl<'a> RewriteContext<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn budget(&self, used_width: usize) -> usize {
|
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 {
|
pub fn inside_macro(&self) -> bool {
|
||||||
|
15
src/shape.rs
15
src/shape.rs
@ -181,7 +181,7 @@ impl Shape {
|
|||||||
|
|
||||||
pub fn indented(indent: Indent, config: &Config) -> Shape {
|
pub fn indented(indent: Indent, config: &Config) -> Shape {
|
||||||
Shape {
|
Shape {
|
||||||
width: config.max_width().checked_sub(indent.width()).unwrap_or(0),
|
width: config.max_width().saturating_sub(indent.width()),
|
||||||
indent,
|
indent,
|
||||||
offset: indent.alignment,
|
offset: indent.alignment,
|
||||||
}
|
}
|
||||||
@ -189,10 +189,7 @@ impl Shape {
|
|||||||
|
|
||||||
pub fn with_max_width(&self, config: &Config) -> Shape {
|
pub fn with_max_width(&self, config: &Config) -> Shape {
|
||||||
Shape {
|
Shape {
|
||||||
width: config
|
width: config.max_width().saturating_sub(self.indent.width()),
|
||||||
.max_width()
|
|
||||||
.checked_sub(self.indent.width())
|
|
||||||
.unwrap_or(0),
|
|
||||||
..*self
|
..*self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -266,17 +263,13 @@ impl Shape {
|
|||||||
pub fn rhs_overhead(&self, config: &Config) -> usize {
|
pub fn rhs_overhead(&self, config: &Config) -> usize {
|
||||||
config
|
config
|
||||||
.max_width()
|
.max_width()
|
||||||
.checked_sub(self.used_width() + self.width)
|
.saturating_sub(self.used_width() + self.width)
|
||||||
.unwrap_or(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn comment(&self, config: &Config) -> Shape {
|
pub fn comment(&self, config: &Config) -> Shape {
|
||||||
let width = min(
|
let width = min(
|
||||||
self.width,
|
self.width,
|
||||||
config
|
config.comment_width().saturating_sub(self.indent.width()),
|
||||||
.comment_width()
|
|
||||||
.checked_sub(self.indent.width())
|
|
||||||
.unwrap_or(0),
|
|
||||||
);
|
);
|
||||||
Shape { width, ..*self }
|
Shape { width, ..*self }
|
||||||
}
|
}
|
||||||
|
@ -29,10 +29,7 @@ pub const SKIP_ANNOTATION: &str = "rustfmt::skip";
|
|||||||
pub fn extra_offset(text: &str, shape: Shape) -> usize {
|
pub fn extra_offset(text: &str, shape: Shape) -> usize {
|
||||||
match text.rfind('\n') {
|
match text.rfind('\n') {
|
||||||
// 1 for newline character
|
// 1 for newline character
|
||||||
Some(idx) => text
|
Some(idx) => text.len().saturating_sub(idx + 1 + shape.used_width()),
|
||||||
.len()
|
|
||||||
.checked_sub(idx + 1 + shape.used_width())
|
|
||||||
.unwrap_or(0),
|
|
||||||
None => text.len(),
|
None => text.len(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -219,9 +219,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
|
|||||||
let item_shape = Shape::indented(item_indent, context.config).sub_width(1)?;
|
let item_shape = Shape::indented(item_indent, context.config).sub_width(1)?;
|
||||||
let (mut field_prefix_max_width, field_prefix_min_width) =
|
let (mut field_prefix_max_width, field_prefix_min_width) =
|
||||||
struct_field_prefix_max_min_width(context, fields, item_shape);
|
struct_field_prefix_max_min_width(context, fields, item_shape);
|
||||||
let max_diff = field_prefix_max_width
|
let max_diff = field_prefix_max_width.saturating_sub(field_prefix_min_width);
|
||||||
.checked_sub(field_prefix_min_width)
|
|
||||||
.unwrap_or(0);
|
|
||||||
if max_diff > context.config.struct_field_align_threshold() {
|
if max_diff > context.config.struct_field_align_threshold() {
|
||||||
field_prefix_max_width = 0;
|
field_prefix_max_width = 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user