1
Fork 0

Remove unnecessary wrap_str()

This commit is contained in:
topecongiro 2017-09-19 11:40:20 +09:00
parent 007c673012
commit b751030640
5 changed files with 45 additions and 67 deletions

View file

@ -36,7 +36,7 @@ use types::{can_be_overflowed_type, rewrite_path, PathContext};
use utils::{colon_spaces, contains_skip, extra_offset, first_line_width, inner_attributes, use utils::{colon_spaces, contains_skip, extra_offset, first_line_width, inner_attributes,
last_line_extendable, last_line_width, left_most_sub_expr, mk_sp, outer_attributes, last_line_extendable, last_line_width, left_most_sub_expr, mk_sp, outer_attributes,
paren_overhead, ptr_vec_to_ref_vec, semicolon_for_stmt, stmt_expr, paren_overhead, ptr_vec_to_ref_vec, semicolon_for_stmt, stmt_expr,
trimmed_last_line_width, wrap_str}; trimmed_last_line_width};
use vertical::rewrite_with_alignment; use vertical::rewrite_with_alignment;
use visitor::FmtVisitor; use visitor::FmtVisitor;
@ -76,11 +76,7 @@ pub fn format_expr(
ast::LitKind::Str(_, ast::StrStyle::Cooked) => { ast::LitKind::Str(_, ast::StrStyle::Cooked) => {
rewrite_string_lit(context, l.span, shape) rewrite_string_lit(context, l.span, shape)
} }
_ => wrap_str( _ => Some(context.snippet(expr.span)),
context.snippet(expr.span),
context.config.max_width(),
shape,
),
}, },
ast::ExprKind::Call(ref callee, ref args) => { ast::ExprKind::Call(ref callee, ref args) => {
let inner_span = mk_sp(callee.span.hi(), expr.span.hi()); let inner_span = mk_sp(callee.span.hi(), expr.span.hi());
@ -153,11 +149,7 @@ pub fn format_expr(
Some(ident) => format!(" {}", ident.node), Some(ident) => format!(" {}", ident.node),
None => String::new(), None => String::new(),
}; };
wrap_str( Some(format!("continue{}", id_str))
format!("continue{}", id_str),
context.config.max_width(),
shape,
)
} }
ast::ExprKind::Break(ref opt_ident, ref opt_expr) => { ast::ExprKind::Break(ref opt_ident, ref opt_expr) => {
let id_str = match *opt_ident { let id_str = match *opt_ident {
@ -168,17 +160,13 @@ pub fn format_expr(
if let Some(ref expr) = *opt_expr { if let Some(ref expr) = *opt_expr {
rewrite_unary_prefix(context, &format!("break{} ", id_str), &**expr, shape) rewrite_unary_prefix(context, &format!("break{} ", id_str), &**expr, shape)
} else { } else {
wrap_str( Some(format!("break{}", id_str))
format!("break{}", id_str),
context.config.max_width(),
shape,
)
} }
} }
ast::ExprKind::Yield(ref opt_expr) => if let Some(ref expr) = *opt_expr { ast::ExprKind::Yield(ref opt_expr) => if let Some(ref expr) = *opt_expr {
rewrite_unary_prefix(context, "yield ", &**expr, shape) rewrite_unary_prefix(context, "yield ", &**expr, shape)
} else { } else {
wrap_str("yield".to_string(), context.config.max_width(), shape) Some("yield".to_string())
}, },
ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => { ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => {
rewrite_closure(capture, fn_decl, body, expr.span, context, shape) rewrite_closure(capture, fn_decl, body, expr.span, context, shape)
@ -190,17 +178,10 @@ pub fn format_expr(
ast::ExprKind::Mac(ref mac) => { ast::ExprKind::Mac(ref mac) => {
// Failure to rewrite a marco should not imply failure to // Failure to rewrite a marco should not imply failure to
// rewrite the expression. // rewrite the expression.
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| { rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
wrap_str( .or_else(|| Some(context.snippet(expr.span)))
context.snippet(expr.span),
context.config.max_width(),
shape,
)
})
}
ast::ExprKind::Ret(None) => {
wrap_str("return".to_owned(), context.config.max_width(), shape)
} }
ast::ExprKind::Ret(None) => Some("return".to_owned()),
ast::ExprKind::Ret(Some(ref expr)) => { ast::ExprKind::Ret(Some(ref expr)) => {
rewrite_unary_prefix(context, "return ", &**expr, shape) rewrite_unary_prefix(context, "return ", &**expr, shape)
} }
@ -302,16 +283,14 @@ pub fn format_expr(
}; };
rewrite_unary_suffix(context, &sp_delim, &*lhs, shape) rewrite_unary_suffix(context, &sp_delim, &*lhs, shape)
} }
(None, None) => wrap_str(delim.into(), context.config.max_width(), shape), (None, None) => Some(delim.into()),
} }
} }
// We do not format these expressions yet, but they should still // We do not format these expressions yet, but they should still
// satisfy our width restrictions. // satisfy our width restrictions.
ast::ExprKind::InPlace(..) | ast::ExprKind::InlineAsm(..) => wrap_str( ast::ExprKind::InPlace(..) | ast::ExprKind::InlineAsm(..) => {
context.snippet(expr.span), Some(context.snippet(expr.span))
context.config.max_width(), }
shape,
),
ast::ExprKind::Catch(ref block) => { ast::ExprKind::Catch(ref block) => {
if let rw @ Some(_) = rewrite_single_line_block(context, "do catch ", block, shape) { if let rw @ Some(_) = rewrite_single_line_block(context, "do catch ", block, shape) {
rw rw
@ -383,7 +362,11 @@ where
.map(|first_line| first_line.ends_with('{')) .map(|first_line| first_line.ends_with('{'))
.unwrap_or(false); .unwrap_or(false);
if !rhs_result.contains('\n') || allow_same_line { if !rhs_result.contains('\n') || allow_same_line {
return Some(format!("{}{}{}{}", lhs_result, infix, rhs_result, suffix)); let one_line_width = last_line_width(&lhs_result) + infix.len()
+ first_line_width(&rhs_result) + suffix.len();
if one_line_width <= shape.width {
return Some(format!("{}{}{}{}", lhs_result, infix, rhs_result, suffix));
}
} }
} }
@ -2665,11 +2648,7 @@ pub fn rewrite_field(
prefix_max_width: usize, prefix_max_width: usize,
) -> Option<String> { ) -> Option<String> {
if contains_skip(&field.attrs) { if contains_skip(&field.attrs) {
return wrap_str( return Some(context.snippet(field.span()));
context.snippet(field.span()),
context.config.max_width(),
shape,
);
} }
let name = &field.ident.node.to_string(); let name = &field.ident.node.to_string();
if field.is_shorthand { if field.is_shorthand {

View file

@ -32,7 +32,7 @@ use utils::{colon_spaces, contains_skip, end_typaram, first_line_width, format_a
format_constness, format_defaultness, format_mutability, format_unsafety, format_constness, format_defaultness, format_mutability, format_unsafety,
format_visibility, is_attributes_extendable, last_line_contains_single_line_comment, format_visibility, is_attributes_extendable, last_line_contains_single_line_comment,
last_line_used_width, last_line_width, mk_sp, semicolon_for_expr, stmt_expr, last_line_used_width, last_line_width, mk_sp, semicolon_for_expr, stmt_expr,
trim_newlines, trimmed_last_line_width, wrap_str}; trim_newlines, trimmed_last_line_width};
use vertical::rewrite_with_alignment; use vertical::rewrite_with_alignment;
use visitor::FmtVisitor; use visitor::FmtVisitor;
@ -1361,8 +1361,7 @@ pub fn rewrite_struct_field(
lhs_max_width: usize, lhs_max_width: usize,
) -> Option<String> { ) -> Option<String> {
if contains_skip(&field.attrs) { if contains_skip(&field.attrs) {
let span = context.snippet(mk_sp(field.attrs[0].span.lo(), field.span.hi())); return Some(context.snippet(mk_sp(field.attrs[0].span.lo(), field.span.hi())));
return wrap_str(span, context.config.max_width(), shape);
} }
let type_annotation_spacing = type_annotation_spacing(context.config); let type_annotation_spacing = type_annotation_spacing(context.config);

View file

@ -24,7 +24,6 @@ extern crate syntax;
extern crate term; extern crate term;
extern crate unicode_segmentation; extern crate unicode_segmentation;
use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::io::{self, stdout, Write}; use std::io::{self, stdout, Write};

View file

@ -22,7 +22,7 @@ use lists::{itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_sh
use rewrite::{Rewrite, RewriteContext}; use rewrite::{Rewrite, RewriteContext};
use shape::Shape; use shape::Shape;
use types::{rewrite_path, PathContext}; use types::{rewrite_path, PathContext};
use utils::{format_mutability, mk_sp, wrap_str}; use utils::{format_mutability, mk_sp};
impl Rewrite for Pat { impl Rewrite for Pat {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
@ -51,8 +51,7 @@ impl Rewrite for Pat {
None => "".to_owned(), None => "".to_owned(),
}; };
let result = format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat); Some(format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat))
wrap_str(result, context.config.max_width(), shape)
} }
PatKind::Wild => if 1 <= shape.width { PatKind::Wild => if 1 <= shape.width {
Some("_".to_owned()) Some("_".to_owned())
@ -125,17 +124,13 @@ impl Rewrite for Pat {
} else { } else {
format!("[{}]", pats.join(", ")) format!("[{}]", pats.join(", "))
}; };
wrap_str(result, context.config.max_width(), shape) Some(result)
} }
PatKind::Struct(ref path, ref fields, elipses) => { PatKind::Struct(ref path, ref fields, elipses) => {
rewrite_struct_pat(path, fields, elipses, self.span, context, shape) rewrite_struct_pat(path, fields, elipses, self.span, context, shape)
} }
// FIXME(#819) format pattern macros. // FIXME(#819) format pattern macros.
PatKind::Mac(..) => wrap_str( PatKind::Mac(..) => Some(context.snippet(self.span)),
context.snippet(self.span),
context.config.max_width(),
shape,
),
} }
} }
} }
@ -225,11 +220,21 @@ impl Rewrite for FieldPat {
if self.is_shorthand { if self.is_shorthand {
pat pat
} else { } else {
wrap_str( let pat_str = try_opt!(pat);
format!("{}: {}", self.ident.to_string(), try_opt!(pat)), let id_str = self.ident.to_string();
context.config.max_width(), let one_line_width = id_str.len() + 2 + pat_str.len();
shape, if one_line_width <= shape.width {
) Some(format!("{}: {}", id_str, pat_str))
} else {
let nested_shape = shape.block_indent(context.config.tab_spaces());
let pat_str = try_opt!(self.pat.rewrite(context, nested_shape));
Some(format!(
"{}:\n{}{}",
id_str,
nested_shape.indent.to_string(context.config),
pat_str,
))
}
} }
} }
} }

View file

@ -26,7 +26,7 @@ use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListTac
SeparatorPlace, SeparatorTactic}; SeparatorPlace, SeparatorTactic};
use rewrite::{Rewrite, RewriteContext}; use rewrite::{Rewrite, RewriteContext};
use shape::Shape; use shape::Shape;
use utils::{colon_spaces, extra_offset, format_mutability, last_line_width, mk_sp, wrap_str}; use utils::{colon_spaces, extra_offset, format_mutability, last_line_width, mk_sp};
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PathContext { pub enum PathContext {
@ -504,7 +504,7 @@ impl Rewrite for ast::WherePredicate {
} }
}; };
wrap_str(result, context.config.max_width(), shape) Some(result)
} }
} }
@ -542,7 +542,7 @@ where
colon, colon,
join_bounds(context, try_opt!(shape.sub_width(overhead)), &appendix) join_bounds(context, try_opt!(shape.sub_width(overhead)), &appendix)
); );
wrap_str(result, context.config.max_width(), shape) Some(result)
} }
} }
@ -565,12 +565,8 @@ impl Rewrite for ast::TyParamBound {
} }
impl Rewrite for ast::Lifetime { impl Rewrite for ast::Lifetime {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> { fn rewrite(&self, _: &RewriteContext, _: Shape) -> Option<String> {
wrap_str( Some(pprust::lifetime_to_string(self))
pprust::lifetime_to_string(self),
context.config.max_width(),
shape,
)
} }
} }
@ -612,7 +608,7 @@ impl Rewrite for ast::TyParam {
result.push_str(&rewrite); result.push_str(&rewrite);
} }
wrap_str(result, context.config.max_width(), shape) Some(result)
} }
} }