refactor: remove some unnecessary clones

This commit is contained in:
Caleb Cartwright 2022-07-03 22:09:42 -05:00 committed by Yacin Tmimi
parent 2ae63f0018
commit 35f4c55bf4
4 changed files with 24 additions and 26 deletions

View File

@ -70,8 +70,8 @@ use crate::rewrite::{Rewrite, RewriteContext};
use crate::shape::Shape; use crate::shape::Shape;
use crate::source_map::SpanUtils; use crate::source_map::SpanUtils;
use crate::utils::{ use crate::utils::{
self, first_line_width, last_line_extendable, last_line_width, mk_sp, rewrite_ident, self, filtered_str_fits, first_line_width, last_line_extendable, last_line_width, mk_sp,
trimmed_last_line_width, wrap_str, rewrite_ident, trimmed_last_line_width, wrap_str,
}; };
pub(crate) fn rewrite_chain( pub(crate) fn rewrite_chain(
@ -810,15 +810,14 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
.visual_indent(self.offset) .visual_indent(self.offset)
.sub_width(self.offset)?; .sub_width(self.offset)?;
let rewrite = item.rewrite(context, child_shape)?; let rewrite = item.rewrite(context, child_shape)?;
match wrap_str(rewrite, context.config.max_width(), shape) { if filtered_str_fits(&rewrite, context.config.max_width(), shape) {
Some(rewrite) => root_rewrite.push_str(&rewrite), root_rewrite.push_str(&rewrite);
None => { } else {
// We couldn't fit in at the visual indent, try the last // We couldn't fit in at the visual indent, try the last
// indent. // indent.
let rewrite = item.rewrite(context, parent_shape)?; let rewrite = item.rewrite(context, parent_shape)?;
root_rewrite.push_str(&rewrite); root_rewrite.push_str(&rewrite);
self.offset = 0; self.offset = 0;
}
} }
self.shared.children = &self.shared.children[1..]; self.shared.children = &self.shared.children[1..];

View File

@ -29,9 +29,9 @@ use crate::spanned::Spanned;
use crate::string::{rewrite_string, StringFormat}; use crate::string::{rewrite_string, StringFormat};
use crate::types::{rewrite_path, PathContext}; use crate::types::{rewrite_path, PathContext};
use crate::utils::{ use crate::utils::{
colon_spaces, contains_skip, count_newlines, first_line_ends_with, inner_attributes, colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with,
last_line_extendable, last_line_width, mk_sp, outer_attributes, semicolon_for_expr, inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
unicode_str_width, wrap_str, semicolon_for_expr, unicode_str_width, wrap_str,
}; };
use crate::vertical::rewrite_with_alignment; use crate::vertical::rewrite_with_alignment;
use crate::visitor::FmtVisitor; use crate::visitor::FmtVisitor;
@ -2037,8 +2037,7 @@ fn choose_rhs<R: Rewrite>(
match (orig_rhs, new_rhs) { match (orig_rhs, new_rhs) {
(Some(ref orig_rhs), Some(ref new_rhs)) (Some(ref orig_rhs), Some(ref new_rhs))
if wrap_str(new_rhs.clone(), context.config.max_width(), new_shape) if !filtered_str_fits(&new_rhs, context.config.max_width(), new_shape) =>
.is_none() =>
{ {
Some(format!("{}{}", before_space_str, orig_rhs)) Some(format!("{}{}", before_space_str, orig_rhs))
} }

View File

@ -35,8 +35,8 @@ use crate::shape::{Indent, Shape};
use crate::source_map::SpanUtils; use crate::source_map::SpanUtils;
use crate::spanned::Spanned; use crate::spanned::Spanned;
use crate::utils::{ use crate::utils::{
format_visibility, indent_next_line, is_empty_line, mk_sp, remove_trailing_white_spaces, filtered_str_fits, format_visibility, indent_next_line, is_empty_line, mk_sp,
rewrite_ident, trim_left_preserve_layout, wrap_str, NodeIdExt, remove_trailing_white_spaces, rewrite_ident, trim_left_preserve_layout, NodeIdExt,
}; };
use crate::visitor::FmtVisitor; use crate::visitor::FmtVisitor;
@ -1241,15 +1241,14 @@ impl MacroBranch {
} }
} }
}; };
let new_body = wrap_str(
new_body_snippet.snippet.to_string(), if !filtered_str_fits(&new_body_snippet.snippet, config.max_width(), shape) {
config.max_width(), return None;
shape, }
)?;
// Indent the body since it is in a block. // Indent the body since it is in a block.
let indent_str = body_indent.to_string(&config); let indent_str = body_indent.to_string(&config);
let mut new_body = LineClasses::new(new_body.trim_end()) let mut new_body = LineClasses::new(new_body_snippet.snippet.trim_end())
.enumerate() .enumerate()
.fold( .fold(
(String::new(), true), (String::new(), true),

View File

@ -384,14 +384,15 @@ macro_rules! skip_out_of_file_lines_range_visitor {
// Wraps String in an Option. Returns Some when the string adheres to the // Wraps String in an Option. Returns Some when the string adheres to the
// Rewrite constraints defined for the Rewrite trait and None otherwise. // Rewrite constraints defined for the Rewrite trait and None otherwise.
pub(crate) fn wrap_str(s: String, max_width: usize, shape: Shape) -> Option<String> { pub(crate) fn wrap_str(s: String, max_width: usize, shape: Shape) -> Option<String> {
if is_valid_str(&filter_normal_code(&s), max_width, shape) { if filtered_str_fits(&s, max_width, shape) {
Some(s) Some(s)
} else { } else {
None None
} }
} }
fn is_valid_str(snippet: &str, max_width: usize, shape: Shape) -> bool { pub(crate) fn filtered_str_fits(snippet: &str, max_width: usize, shape: Shape) -> bool {
let snippet = &filter_normal_code(snippet);
if !snippet.is_empty() { if !snippet.is_empty() {
// First line must fits with `shape.width`. // First line must fits with `shape.width`.
if first_line_width(snippet) > shape.width { if first_line_width(snippet) > shape.width {