Merge pull request #1469 from topecongiro/bug/chains

Remove a bug when calculating the length of the chain
This commit is contained in:
Nick Cameron 2017-05-01 15:48:53 +12:00 committed by GitHub
commit fba5af2a7d
11 changed files with 26 additions and 87 deletions

View File

@ -169,7 +169,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
let almost_total = rewrites[..rewrites.len() - 1]
.iter()
.fold(0, |a, b| a + first_line_width(b)) + parent_rewrite.len();
let one_line_len = rewrites.iter().fold(0, |a, r| a + r.len() + 1) + parent_rewrite.len();
let one_line_len = rewrites.iter().fold(0, |a, r| a + r.len()) + parent_rewrite.len();
let veto_single_line = if one_line_len > context.config.chain_one_line_max - 1 &&
rewrites.len() > 1 {
@ -424,10 +424,8 @@ fn rewrite_method_call(method_name: ast::Ident,
let (lo, type_str) = if types.is_empty() {
(args[0].span.hi, String::new())
} else {
let type_list: Vec<_> = try_opt!(types
.iter()
.map(|ty| ty.rewrite(context, shape))
.collect());
let type_list: Vec<_> =
try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect());
let type_str = if context.config.spaces_within_angle_brackets && type_list.len() > 0 {
format!("::< {} >", type_list.join(", "))

View File

@ -99,10 +99,7 @@ pub fn rewrite_comment(orig: &str,
config: config,
};
let line_breaks = orig.trim_right()
.chars()
.filter(|&c| c == '\n')
.count();
let line_breaks = orig.trim_right().chars().filter(|&c| c == '\n').count();
let lines = orig.lines()
.enumerate()
.map(|(i, mut line)| {

View File

@ -600,9 +600,7 @@ fn and_one_line(x: Option<String>) -> Option<String> {
fn nop_block_collapse(block_str: Option<String>, budget: usize) -> Option<String> {
debug!("nop_block_collapse {:?} {}", block_str, budget);
block_str.map(|block_str| if block_str.starts_with('{') && budget >= 2 &&
(block_str[1..]
.find(|c: char| !c.is_whitespace())
.unwrap() ==
(block_str[1..].find(|c: char| !c.is_whitespace()).unwrap() ==
block_str.len() - 2) {
"{}".to_owned()
} else {
@ -933,16 +931,12 @@ impl<'a> Rewrite for ControlFlow<'a> {
// for event in event
let between_kwd_cond =
mk_sp(context
.codemap
.span_after(self.span, self.keyword.trim()),
mk_sp(context.codemap.span_after(self.span, self.keyword.trim()),
self.pat
.map_or(cond_span.lo, |p| if self.matcher.is_empty() {
p.span.lo
} else {
context
.codemap
.span_before(self.span, self.matcher.trim())
context.codemap.span_before(self.span, self.matcher.trim())
}));
let between_kwd_cond_comment = extract_comment(between_kwd_cond, context, shape);
@ -1137,10 +1131,7 @@ fn rewrite_match_arm_comment(context: &RewriteContext,
let first = missed_str
.find(|c: char| !c.is_whitespace())
.unwrap_or(missed_str.len());
if missed_str[..first]
.chars()
.filter(|c| c == &'\n')
.count() >= 2 {
if missed_str[..first].chars().filter(|c| c == &'\n').count() >= 2 {
// Excessive vertical whitespace before comment should be preserved
// FIXME handle vertical whitespace better
result.push('\n');

View File

@ -29,10 +29,7 @@ fn path_of(a: &ast::ViewPath_) -> &ast::Path {
}
fn compare_path_segments(a: &ast::PathSegment, b: &ast::PathSegment) -> Ordering {
a.identifier
.name
.as_str()
.cmp(&b.identifier.name.as_str())
a.identifier.name.as_str().cmp(&b.identifier.name.as_str())
}
fn compare_paths(a: &ast::Path, b: &ast::Path) -> Ordering {

View File

@ -242,9 +242,7 @@ impl<'a> FmtVisitor<'a> {
let context = self.get_context();
let block_snippet = self.snippet(codemap::mk_sp(block.span.lo, block.span.hi));
let has_body = !block_snippet[1..block_snippet.len() - 1]
.trim()
.is_empty() ||
let has_body = !block_snippet[1..block_snippet.len() - 1].trim().is_empty() ||
!context.config.fn_empty_single_line;
let (mut result, force_newline_brace) = try_opt!(rewrite_fn_base(&context,
@ -1227,10 +1225,7 @@ impl Rewrite for ast::StructField {
let type_offset = shape.indent.block_indent(context.config);
let rewrite_type_in_next_line = || {
let budget = try_opt!(context
.config
.max_width
.checked_sub(type_offset.width()));
let budget = try_opt!(context.config.max_width.checked_sub(type_offset.width()));
self.ty
.rewrite(context, Shape::legacy(budget, type_offset))
};
@ -1624,9 +1619,7 @@ fn rewrite_fn_base(context: &RewriteContext,
.ty_params
.last()
.map_or(span.lo, |tp| end_typaram(tp));
let args_span = mk_sp(context
.codemap
.span_after(mk_sp(args_start, span.hi), "("),
let args_span = mk_sp(context.codemap.span_after(mk_sp(args_start, span.hi), "("),
span_for_return(&fd.output).lo);
let arg_str = try_opt!(rewrite_args(context,
&fd.inputs,
@ -1767,10 +1760,7 @@ fn rewrite_fn_base(context: &RewriteContext,
}
}
let budget = try_opt!(context
.config
.max_width
.checked_sub(indent.block_indent));
let budget = try_opt!(context.config.max_width.checked_sub(indent.block_indent));
let where_clause_str = try_opt!(rewrite_where_clause(context,
where_clause,
context.config.fn_brace_style,

View File

@ -172,9 +172,7 @@ pub fn definitive_tactic<I, T>(items: I, tactic: ListTactic, width: usize) -> De
let real_total = total_width + total_sep_len;
if real_total <= limit && !pre_line_comments &&
!items
.into_iter()
.any(|item| item.as_ref().is_multiline()) {
!items.into_iter().any(|item| item.as_ref().is_multiline()) {
DefinitiveListTactic::Horizontal
} else {
DefinitiveListTactic::Vertical
@ -536,10 +534,7 @@ pub fn struct_lit_shape(shape: Shape,
IndentStyle::Block => {
let shape = shape.block_indent(context.config.tab_spaces);
Shape {
width: try_opt!(context
.config
.max_width
.checked_sub(shape.indent.width())),
width: try_opt!(context.config.max_width.checked_sub(shape.indent.width())),
..shape
}
}
@ -556,12 +551,7 @@ pub fn struct_lit_tactic(h_shape: Option<Shape>,
if let Some(h_shape) = h_shape {
let mut prelim_tactic = match (context.config.struct_lit_style, items.len()) {
(IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
_ => {
context
.config
.struct_lit_multiline_style
.to_list_tactic()
}
_ => context.config.struct_lit_multiline_style.to_list_tactic(),
};
if prelim_tactic == ListTactic::HorizontalVertical && items.len() > 1 {

View File

@ -196,15 +196,9 @@ pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option<ast::
fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
let snippet = context.snippet(mac.span);
let paren_pos = snippet
.find_uncommented("(")
.unwrap_or(usize::max_value());
let bracket_pos = snippet
.find_uncommented("[")
.unwrap_or(usize::max_value());
let brace_pos = snippet
.find_uncommented("{")
.unwrap_or(usize::max_value());
let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value());
let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value());
let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::max_value());
if paren_pos < bracket_pos && paren_pos < brace_pos {
MacroStyle::Parens

View File

@ -102,10 +102,8 @@ impl Rewrite for Pat {
let suffix = suffix.iter().map(|p| p.rewrite(context, shape));
// Munge them together.
let pats: Option<Vec<String>> = prefix
.chain(slice_pat.into_iter())
.chain(suffix)
.collect();
let pats: Option<Vec<String>> =
prefix.chain(slice_pat.into_iter()).chain(suffix).collect();
// Check that all the rewrites succeeded, and if not return None.
let pats = try_opt!(pats);
@ -244,9 +242,7 @@ fn rewrite_tuple_pat(pats: &[ptr::P<ast::Pat>],
context: &RewriteContext,
shape: Shape)
-> Option<String> {
let mut pat_vec: Vec<_> = pats.into_iter()
.map(|x| TuplePatField::Pat(x))
.collect();
let mut pat_vec: Vec<_> = pats.into_iter().map(|x| TuplePatField::Pat(x)).collect();
if let Some(pos) = dotdot_pos {
let prev = if pos == 0 {

View File

@ -28,9 +28,7 @@ const DIFF_CONTEXT_SIZE: usize = 3;
fn get_path_string(dir_entry: io::Result<fs::DirEntry>) -> String {
let path = dir_entry.expect("Couldn't get DirEntry").path();
path.to_str()
.expect("Couldn't stringify path")
.to_owned()
path.to_str().expect("Couldn't stringify path").to_owned()
}
// Integration tests. The files in the tests/source are formatted and compared
@ -337,8 +335,7 @@ fn handle_result(result: HashMap<String, String>,
let mut f = fs::File::open(&target).expect("Couldn't open target");
let mut text = String::new();
f.read_to_string(&mut text)
.expect("Failed reading target");
f.read_to_string(&mut text).expect("Failed reading target");
if fmt_text != text {
let diff = make_diff(&text, &fmt_text, DIFF_CONTEXT_SIZE);

View File

@ -137,16 +137,7 @@ fn try_shorthand() {
let y = expr.kaas()?.test();
let loooooooooooooooooooooooooooooooooooooooooong =
does_this?.look?.good?.should_we_break?.after_the_first_question_mark?;
let yyyy = expr?
.another?
.another?
.another?
.another?
.another?
.another?
.another?
.another?
.test();
let yyyy = expr?.another?.another?.another?.another?.another?.another?.another?.another?.test();
let zzzz = expr?.another?.another?.another?.another?;
let aaa = x??????????????????????????????????????????????????????????????????????????;

View File

@ -1,6 +1,4 @@
fn f() {
block_flow
.base
.stacking_relative_position_of_display_port =
block_flow.base.stacking_relative_position_of_display_port =
self.base.stacking_relative_position_of_display_port;
}