Skip to content

Commit 35f4c55

Browse files
calebcartwrightytmimi
authored andcommitted
refactor: remove some unnecessary clones
1 parent 2ae63f0 commit 35f4c55

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

src/chains.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ use crate::rewrite::{Rewrite, RewriteContext};
7070
use crate::shape::Shape;
7171
use crate::source_map::SpanUtils;
7272
use crate::utils::{
73-
self, first_line_width, last_line_extendable, last_line_width, mk_sp, rewrite_ident,
74-
trimmed_last_line_width, wrap_str,
73+
self, filtered_str_fits, first_line_width, last_line_extendable, last_line_width, mk_sp,
74+
rewrite_ident, trimmed_last_line_width, wrap_str,
7575
};
7676

7777
pub(crate) fn rewrite_chain(
@@ -810,15 +810,14 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
810810
.visual_indent(self.offset)
811811
.sub_width(self.offset)?;
812812
let rewrite = item.rewrite(context, child_shape)?;
813-
match wrap_str(rewrite, context.config.max_width(), shape) {
814-
Some(rewrite) => root_rewrite.push_str(&rewrite),
815-
None => {
816-
// We couldn't fit in at the visual indent, try the last
817-
// indent.
818-
let rewrite = item.rewrite(context, parent_shape)?;
819-
root_rewrite.push_str(&rewrite);
820-
self.offset = 0;
821-
}
813+
if filtered_str_fits(&rewrite, context.config.max_width(), shape) {
814+
root_rewrite.push_str(&rewrite);
815+
} else {
816+
// We couldn't fit in at the visual indent, try the last
817+
// indent.
818+
let rewrite = item.rewrite(context, parent_shape)?;
819+
root_rewrite.push_str(&rewrite);
820+
self.offset = 0;
822821
}
823822

824823
self.shared.children = &self.shared.children[1..];

src/expr.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ use crate::spanned::Spanned;
2929
use crate::string::{rewrite_string, StringFormat};
3030
use crate::types::{rewrite_path, PathContext};
3131
use crate::utils::{
32-
colon_spaces, contains_skip, count_newlines, first_line_ends_with, inner_attributes,
33-
last_line_extendable, last_line_width, mk_sp, outer_attributes, semicolon_for_expr,
34-
unicode_str_width, wrap_str,
32+
colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with,
33+
inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
34+
semicolon_for_expr, unicode_str_width, wrap_str,
3535
};
3636
use crate::vertical::rewrite_with_alignment;
3737
use crate::visitor::FmtVisitor;
@@ -2037,8 +2037,7 @@ fn choose_rhs<R: Rewrite>(
20372037

20382038
match (orig_rhs, new_rhs) {
20392039
(Some(ref orig_rhs), Some(ref new_rhs))
2040-
if wrap_str(new_rhs.clone(), context.config.max_width(), new_shape)
2041-
.is_none() =>
2040+
if !filtered_str_fits(&new_rhs, context.config.max_width(), new_shape) =>
20422041
{
20432042
Some(format!("{}{}", before_space_str, orig_rhs))
20442043
}

src/macros.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ use crate::shape::{Indent, Shape};
3535
use crate::source_map::SpanUtils;
3636
use crate::spanned::Spanned;
3737
use crate::utils::{
38-
format_visibility, indent_next_line, is_empty_line, mk_sp, remove_trailing_white_spaces,
39-
rewrite_ident, trim_left_preserve_layout, wrap_str, NodeIdExt,
38+
filtered_str_fits, format_visibility, indent_next_line, is_empty_line, mk_sp,
39+
remove_trailing_white_spaces, rewrite_ident, trim_left_preserve_layout, NodeIdExt,
4040
};
4141
use crate::visitor::FmtVisitor;
4242

@@ -1241,15 +1241,14 @@ impl MacroBranch {
12411241
}
12421242
}
12431243
};
1244-
let new_body = wrap_str(
1245-
new_body_snippet.snippet.to_string(),
1246-
config.max_width(),
1247-
shape,
1248-
)?;
1244+
1245+
if !filtered_str_fits(&new_body_snippet.snippet, config.max_width(), shape) {
1246+
return None;
1247+
}
12491248

12501249
// Indent the body since it is in a block.
12511250
let indent_str = body_indent.to_string(&config);
1252-
let mut new_body = LineClasses::new(new_body.trim_end())
1251+
let mut new_body = LineClasses::new(new_body_snippet.snippet.trim_end())
12531252
.enumerate()
12541253
.fold(
12551254
(String::new(), true),

src/utils.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -384,14 +384,15 @@ macro_rules! skip_out_of_file_lines_range_visitor {
384384
// Wraps String in an Option. Returns Some when the string adheres to the
385385
// Rewrite constraints defined for the Rewrite trait and None otherwise.
386386
pub(crate) fn wrap_str(s: String, max_width: usize, shape: Shape) -> Option<String> {
387-
if is_valid_str(&filter_normal_code(&s), max_width, shape) {
387+
if filtered_str_fits(&s, max_width, shape) {
388388
Some(s)
389389
} else {
390390
None
391391
}
392392
}
393393

394-
fn is_valid_str(snippet: &str, max_width: usize, shape: Shape) -> bool {
394+
pub(crate) fn filtered_str_fits(snippet: &str, max_width: usize, shape: Shape) -> bool {
395+
let snippet = &filter_normal_code(snippet);
395396
if !snippet.is_empty() {
396397
// First line must fits with `shape.width`.
397398
if first_line_width(snippet) > shape.width {

0 commit comments

Comments
 (0)