Skip to content

Commit b751030

Browse files
committed
Remove unnecessary wrap_str()
1 parent 007c673 commit b751030

File tree

5 files changed

+45
-67
lines changed

5 files changed

+45
-67
lines changed

src/expr.rs

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use types::{can_be_overflowed_type, rewrite_path, PathContext};
3636
use utils::{colon_spaces, contains_skip, extra_offset, first_line_width, inner_attributes,
3737
last_line_extendable, last_line_width, left_most_sub_expr, mk_sp, outer_attributes,
3838
paren_overhead, ptr_vec_to_ref_vec, semicolon_for_stmt, stmt_expr,
39-
trimmed_last_line_width, wrap_str};
39+
trimmed_last_line_width};
4040
use vertical::rewrite_with_alignment;
4141
use visitor::FmtVisitor;
4242

@@ -76,11 +76,7 @@ pub fn format_expr(
7676
ast::LitKind::Str(_, ast::StrStyle::Cooked) => {
7777
rewrite_string_lit(context, l.span, shape)
7878
}
79-
_ => wrap_str(
80-
context.snippet(expr.span),
81-
context.config.max_width(),
82-
shape,
83-
),
79+
_ => Some(context.snippet(expr.span)),
8480
},
8581
ast::ExprKind::Call(ref callee, ref args) => {
8682
let inner_span = mk_sp(callee.span.hi(), expr.span.hi());
@@ -153,11 +149,7 @@ pub fn format_expr(
153149
Some(ident) => format!(" {}", ident.node),
154150
None => String::new(),
155151
};
156-
wrap_str(
157-
format!("continue{}", id_str),
158-
context.config.max_width(),
159-
shape,
160-
)
152+
Some(format!("continue{}", id_str))
161153
}
162154
ast::ExprKind::Break(ref opt_ident, ref opt_expr) => {
163155
let id_str = match *opt_ident {
@@ -168,17 +160,13 @@ pub fn format_expr(
168160
if let Some(ref expr) = *opt_expr {
169161
rewrite_unary_prefix(context, &format!("break{} ", id_str), &**expr, shape)
170162
} else {
171-
wrap_str(
172-
format!("break{}", id_str),
173-
context.config.max_width(),
174-
shape,
175-
)
163+
Some(format!("break{}", id_str))
176164
}
177165
}
178166
ast::ExprKind::Yield(ref opt_expr) => if let Some(ref expr) = *opt_expr {
179167
rewrite_unary_prefix(context, "yield ", &**expr, shape)
180168
} else {
181-
wrap_str("yield".to_string(), context.config.max_width(), shape)
169+
Some("yield".to_string())
182170
},
183171
ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => {
184172
rewrite_closure(capture, fn_decl, body, expr.span, context, shape)
@@ -190,17 +178,10 @@ pub fn format_expr(
190178
ast::ExprKind::Mac(ref mac) => {
191179
// Failure to rewrite a marco should not imply failure to
192180
// rewrite the expression.
193-
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
194-
wrap_str(
195-
context.snippet(expr.span),
196-
context.config.max_width(),
197-
shape,
198-
)
199-
})
200-
}
201-
ast::ExprKind::Ret(None) => {
202-
wrap_str("return".to_owned(), context.config.max_width(), shape)
181+
rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
182+
.or_else(|| Some(context.snippet(expr.span)))
203183
}
184+
ast::ExprKind::Ret(None) => Some("return".to_owned()),
204185
ast::ExprKind::Ret(Some(ref expr)) => {
205186
rewrite_unary_prefix(context, "return ", &**expr, shape)
206187
}
@@ -302,16 +283,14 @@ pub fn format_expr(
302283
};
303284
rewrite_unary_suffix(context, &sp_delim, &*lhs, shape)
304285
}
305-
(None, None) => wrap_str(delim.into(), context.config.max_width(), shape),
286+
(None, None) => Some(delim.into()),
306287
}
307288
}
308289
// We do not format these expressions yet, but they should still
309290
// satisfy our width restrictions.
310-
ast::ExprKind::InPlace(..) | ast::ExprKind::InlineAsm(..) => wrap_str(
311-
context.snippet(expr.span),
312-
context.config.max_width(),
313-
shape,
314-
),
291+
ast::ExprKind::InPlace(..) | ast::ExprKind::InlineAsm(..) => {
292+
Some(context.snippet(expr.span))
293+
}
315294
ast::ExprKind::Catch(ref block) => {
316295
if let rw @ Some(_) = rewrite_single_line_block(context, "do catch ", block, shape) {
317296
rw
@@ -383,7 +362,11 @@ where
383362
.map(|first_line| first_line.ends_with('{'))
384363
.unwrap_or(false);
385364
if !rhs_result.contains('\n') || allow_same_line {
386-
return Some(format!("{}{}{}{}", lhs_result, infix, rhs_result, suffix));
365+
let one_line_width = last_line_width(&lhs_result) + infix.len()
366+
+ first_line_width(&rhs_result) + suffix.len();
367+
if one_line_width <= shape.width {
368+
return Some(format!("{}{}{}{}", lhs_result, infix, rhs_result, suffix));
369+
}
387370
}
388371
}
389372

@@ -2665,11 +2648,7 @@ pub fn rewrite_field(
26652648
prefix_max_width: usize,
26662649
) -> Option<String> {
26672650
if contains_skip(&field.attrs) {
2668-
return wrap_str(
2669-
context.snippet(field.span()),
2670-
context.config.max_width(),
2671-
shape,
2672-
);
2651+
return Some(context.snippet(field.span()));
26732652
}
26742653
let name = &field.ident.node.to_string();
26752654
if field.is_shorthand {

src/items.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use utils::{colon_spaces, contains_skip, end_typaram, first_line_width, format_a
3232
format_constness, format_defaultness, format_mutability, format_unsafety,
3333
format_visibility, is_attributes_extendable, last_line_contains_single_line_comment,
3434
last_line_used_width, last_line_width, mk_sp, semicolon_for_expr, stmt_expr,
35-
trim_newlines, trimmed_last_line_width, wrap_str};
35+
trim_newlines, trimmed_last_line_width};
3636
use vertical::rewrite_with_alignment;
3737
use visitor::FmtVisitor;
3838

@@ -1361,8 +1361,7 @@ pub fn rewrite_struct_field(
13611361
lhs_max_width: usize,
13621362
) -> Option<String> {
13631363
if contains_skip(&field.attrs) {
1364-
let span = context.snippet(mk_sp(field.attrs[0].span.lo(), field.span.hi()));
1365-
return wrap_str(span, context.config.max_width(), shape);
1364+
return Some(context.snippet(mk_sp(field.attrs[0].span.lo(), field.span.hi())));
13661365
}
13671366

13681367
let type_annotation_spacing = type_annotation_spacing(context.config);

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ extern crate syntax;
2424
extern crate term;
2525
extern crate unicode_segmentation;
2626

27-
use std::borrow::Cow;
2827
use std::collections::HashMap;
2928
use std::fmt;
3029
use std::io::{self, stdout, Write};

src/patterns.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use lists::{itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_sh
2222
use rewrite::{Rewrite, RewriteContext};
2323
use shape::Shape;
2424
use types::{rewrite_path, PathContext};
25-
use utils::{format_mutability, mk_sp, wrap_str};
25+
use utils::{format_mutability, mk_sp};
2626

2727
impl Rewrite for Pat {
2828
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
@@ -51,8 +51,7 @@ impl Rewrite for Pat {
5151
None => "".to_owned(),
5252
};
5353

54-
let result = format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat);
55-
wrap_str(result, context.config.max_width(), shape)
54+
Some(format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat))
5655
}
5756
PatKind::Wild => if 1 <= shape.width {
5857
Some("_".to_owned())
@@ -125,17 +124,13 @@ impl Rewrite for Pat {
125124
} else {
126125
format!("[{}]", pats.join(", "))
127126
};
128-
wrap_str(result, context.config.max_width(), shape)
127+
Some(result)
129128
}
130129
PatKind::Struct(ref path, ref fields, elipses) => {
131130
rewrite_struct_pat(path, fields, elipses, self.span, context, shape)
132131
}
133132
// FIXME(#819) format pattern macros.
134-
PatKind::Mac(..) => wrap_str(
135-
context.snippet(self.span),
136-
context.config.max_width(),
137-
shape,
138-
),
133+
PatKind::Mac(..) => Some(context.snippet(self.span)),
139134
}
140135
}
141136
}
@@ -225,11 +220,21 @@ impl Rewrite for FieldPat {
225220
if self.is_shorthand {
226221
pat
227222
} else {
228-
wrap_str(
229-
format!("{}: {}", self.ident.to_string(), try_opt!(pat)),
230-
context.config.max_width(),
231-
shape,
232-
)
223+
let pat_str = try_opt!(pat);
224+
let id_str = self.ident.to_string();
225+
let one_line_width = id_str.len() + 2 + pat_str.len();
226+
if one_line_width <= shape.width {
227+
Some(format!("{}: {}", id_str, pat_str))
228+
} else {
229+
let nested_shape = shape.block_indent(context.config.tab_spaces());
230+
let pat_str = try_opt!(self.pat.rewrite(context, nested_shape));
231+
Some(format!(
232+
"{}:\n{}{}",
233+
id_str,
234+
nested_shape.indent.to_string(context.config),
235+
pat_str,
236+
))
237+
}
233238
}
234239
}
235240
}

src/types.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListTac
2626
SeparatorPlace, SeparatorTactic};
2727
use rewrite::{Rewrite, RewriteContext};
2828
use shape::Shape;
29-
use utils::{colon_spaces, extra_offset, format_mutability, last_line_width, mk_sp, wrap_str};
29+
use utils::{colon_spaces, extra_offset, format_mutability, last_line_width, mk_sp};
3030

3131
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
3232
pub enum PathContext {
@@ -504,7 +504,7 @@ impl Rewrite for ast::WherePredicate {
504504
}
505505
};
506506

507-
wrap_str(result, context.config.max_width(), shape)
507+
Some(result)
508508
}
509509
}
510510

@@ -542,7 +542,7 @@ where
542542
colon,
543543
join_bounds(context, try_opt!(shape.sub_width(overhead)), &appendix)
544544
);
545-
wrap_str(result, context.config.max_width(), shape)
545+
Some(result)
546546
}
547547
}
548548

@@ -565,12 +565,8 @@ impl Rewrite for ast::TyParamBound {
565565
}
566566

567567
impl Rewrite for ast::Lifetime {
568-
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
569-
wrap_str(
570-
pprust::lifetime_to_string(self),
571-
context.config.max_width(),
572-
shape,
573-
)
568+
fn rewrite(&self, _: &RewriteContext, _: Shape) -> Option<String> {
569+
Some(pprust::lifetime_to_string(self))
574570
}
575571
}
576572

@@ -612,7 +608,7 @@ impl Rewrite for ast::TyParam {
612608
result.push_str(&rewrite);
613609
}
614610

615-
wrap_str(result, context.config.max_width(), shape)
611+
Some(result)
616612
}
617613
}
618614

0 commit comments

Comments
 (0)