diff --git a/src/chains.rs b/src/chains.rs index 4ffb297e461..3c5f174a0c2 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -249,13 +249,11 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) - // True if the chain is only `?`s. fn chain_only_try(exprs: &[ast::Expr]) -> bool { - exprs - .iter() - .all(|e| if let ast::ExprKind::Try(_) = e.node { - true - } else { - false - }) + exprs.iter().all(|e| if let ast::ExprKind::Try(_) = e.node { + true + } else { + false + }) } pub fn rewrite_try(expr: &ast::Expr, diff --git a/src/expr.rs b/src/expr.rs index 7fb71c2e8b4..dfe519782e1 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1028,11 +1028,10 @@ impl<'a> Rewrite for ControlFlow<'a> { let between_kwd_else_block_comment = extract_comment(between_kwd_else_block, context, shape); - let after_else = - mk_sp(context - .codemap - .span_after(mk_sp(self.block.span.hi, else_block.span.lo), "else"), - else_block.span.lo); + let after_else = mk_sp(context.codemap.span_after(mk_sp(self.block.span.hi, + else_block.span.lo), + "else"), + else_block.span.lo); let after_else_comment = extract_comment(after_else, context, shape); let between_sep = match context.config.control_brace_style { diff --git a/src/items.rs b/src/items.rs index 5eb1414c2e0..b41b6166eca 100644 --- a/src/items.rs +++ b/src/items.rs @@ -457,11 +457,8 @@ impl<'a> FmtVisitor<'a> { } let indent = self.block_indent; - let mut result = try_opt!(field - .node - .attrs - .rewrite(&self.get_context(), - Shape::indented(indent, self.config))); + let mut result = try_opt!(field.node.attrs.rewrite(&self.get_context(), + Shape::indented(indent, self.config))); if !result.is_empty() { result.push('\n'); result.push_str(&indent.to_string(self.config)); @@ -1201,9 +1198,9 @@ impl Rewrite for ast::StructField { let name = self.ident; let vis = format_visibility(&self.vis); - let mut attr_str = - try_opt!(self.attrs - .rewrite(context, Shape::indented(shape.indent, context.config))); + let mut attr_str = try_opt!(self.attrs.rewrite(context, + Shape::indented(shape.indent, + context.config))); if !attr_str.is_empty() { attr_str.push('\n'); attr_str.push_str(&shape.indent.to_string(context.config)); @@ -1223,9 +1220,9 @@ impl Rewrite for ast::StructField { let last_line_width = last_line_width(&result) + type_annotation_spacing.1.len(); let budget = try_opt!(shape.width.checked_sub(last_line_width)); - let ty_rewritten = self.ty - .rewrite(context, - Shape::legacy(budget, shape.indent + last_line_width)); + let ty_rewritten = + self.ty.rewrite(context, + Shape::legacy(budget, shape.indent + last_line_width)); match ty_rewritten { Some(ref ty) if ty.contains('\n') => { let new_ty = rewrite_type_in_next_line(); @@ -1377,9 +1374,8 @@ impl Rewrite for ast::FunctionRetTy { impl Rewrite for ast::Arg { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { if is_named_arg(self) { - let mut result = try_opt!(self.pat - .rewrite(context, - Shape::legacy(shape.width, shape.indent))); + let mut result = try_opt!(self.pat.rewrite(context, + Shape::legacy(shape.width, shape.indent))); if self.ty.node != ast::TyKind::Infer { if context.config.space_before_type_annotation { @@ -1390,10 +1386,9 @@ impl Rewrite for ast::Arg { result.push_str(" "); } let max_width = try_opt!(shape.width.checked_sub(result.len())); - let ty_str = try_opt!(self.ty - .rewrite(context, - Shape::legacy(max_width, - shape.indent + result.len()))); + let ty_str = try_opt!(self.ty.rewrite(context, + Shape::legacy(max_width, + shape.indent + result.len()))); result.push_str(&ty_str); } @@ -1693,9 +1688,8 @@ fn rewrite_fn_base(context: &RewriteContext, if multi_line_ret_str || ret_should_indent { // Now that we know the proper indent and width, we need to // re-layout the return type. - let ret_str = try_opt!(fd.output - .rewrite(context, - Shape::indented(ret_indent, context.config))); + let ret_str = try_opt!(fd.output.rewrite(context, + Shape::indented(ret_indent, context.config))); result.push_str(&ret_str); } else { result.push_str(&ret_str); @@ -1985,16 +1979,14 @@ fn rewrite_generics(context: &RewriteContext, .map(|ty_param| ty_param.rewrite(context, Shape::legacy(h_budget, offset))); // Extract comments between generics. - let lt_spans = lifetimes - .iter() - .map(|l| { - let hi = if l.bounds.is_empty() { - l.lifetime.span.hi - } else { - l.bounds[l.bounds.len() - 1].span.hi - }; - mk_sp(l.lifetime.span.lo, hi) - }); + let lt_spans = lifetimes.iter().map(|l| { + let hi = if l.bounds.is_empty() { + l.lifetime.span.hi + } else { + l.bounds[l.bounds.len() - 1].span.hi + }; + mk_sp(l.lifetime.span.lo, hi) + }); let ty_spans = tys.iter().map(span_for_ty_param); let items = itemize_list(context.codemap, diff --git a/src/lists.rs b/src/lists.rs index 7933ebab807..c2e98de55ed 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -339,116 +339,114 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3> fn next(&mut self) -> Option { let white_space: &[_] = &[' ', '\t']; - self.inner - .next() - .map(|item| { - let mut new_lines = false; - // Pre-comment - let pre_snippet = self.codemap - .span_to_snippet(codemap::mk_sp(self.prev_span_end, (self.get_lo)(&item))) - .unwrap(); - let trimmed_pre_snippet = pre_snippet.trim(); - let has_pre_comment = trimmed_pre_snippet.contains("//") || - trimmed_pre_snippet.contains("/*"); - let pre_comment = if has_pre_comment { - Some(trimmed_pre_snippet.to_owned()) - } else { - None - }; + self.inner.next().map(|item| { + let mut new_lines = false; + // Pre-comment + let pre_snippet = self.codemap + .span_to_snippet(codemap::mk_sp(self.prev_span_end, (self.get_lo)(&item))) + .unwrap(); + let trimmed_pre_snippet = pre_snippet.trim(); + let has_pre_comment = trimmed_pre_snippet.contains("//") || + trimmed_pre_snippet.contains("/*"); + let pre_comment = if has_pre_comment { + Some(trimmed_pre_snippet.to_owned()) + } else { + None + }; - // Post-comment - let next_start = match self.inner.peek() { - Some(next_item) => (self.get_lo)(next_item), - None => self.next_span_start, - }; - let post_snippet = self.codemap - .span_to_snippet(codemap::mk_sp((self.get_hi)(&item), next_start)) - .unwrap(); + // Post-comment + let next_start = match self.inner.peek() { + Some(next_item) => (self.get_lo)(next_item), + None => self.next_span_start, + }; + let post_snippet = self.codemap + .span_to_snippet(codemap::mk_sp((self.get_hi)(&item), next_start)) + .unwrap(); - let comment_end = match self.inner.peek() { - Some(..) => { - let mut block_open_index = post_snippet.find("/*"); - // check if it realy is a block comment (and not //*) - if let Some(i) = block_open_index { - if i > 0 && &post_snippet[i - 1..i] == "/" { - block_open_index = None; - } - } - let newline_index = post_snippet.find('\n'); - let separator_index = post_snippet.find_uncommented(",").unwrap(); - - match (block_open_index, newline_index) { - // Separator before comment, with the next item on same line. - // Comment belongs to next item. - (Some(i), None) if i > separator_index => separator_index + 1, - // Block-style post-comment before the separator. - (Some(i), None) => { - cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i, - separator_index + 1) - } - // Block-style post-comment. Either before or after the separator. - (Some(i), Some(j)) if i < j => { - cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i, - separator_index + 1) - } - // Potential *single* line comment. - (_, Some(j)) if j > separator_index => j + 1, - _ => post_snippet.len(), + let comment_end = match self.inner.peek() { + Some(..) => { + let mut block_open_index = post_snippet.find("/*"); + // check if it realy is a block comment (and not //*) + if let Some(i) = block_open_index { + if i > 0 && &post_snippet[i - 1..i] == "/" { + block_open_index = None; } } - None => { - post_snippet - .find_uncommented(self.terminator) - .unwrap_or(post_snippet.len()) - } - }; + let newline_index = post_snippet.find('\n'); + let separator_index = post_snippet.find_uncommented(",").unwrap(); - if !post_snippet.is_empty() && comment_end > 0 { - // Account for extra whitespace between items. This is fiddly - // because of the way we divide pre- and post- comments. - - // Everything from the separator to the next item. - let test_snippet = &post_snippet[comment_end - 1..]; - let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len()); - // From the end of the first line of comments. - let test_snippet = &test_snippet[first_newline..]; - let first = test_snippet - .find(|c: char| !c.is_whitespace()) - .unwrap_or(test_snippet.len()); - // From the end of the first line of comments to the next non-whitespace char. - let test_snippet = &test_snippet[..first]; - - if test_snippet.chars().filter(|c| c == &'\n').count() > 1 { - // There were multiple line breaks which got trimmed to nothing. - new_lines = true; + match (block_open_index, newline_index) { + // Separator before comment, with the next item on same line. + // Comment belongs to next item. + (Some(i), None) if i > separator_index => separator_index + 1, + // Block-style post-comment before the separator. + (Some(i), None) => { + cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i, + separator_index + 1) + } + // Block-style post-comment. Either before or after the separator. + (Some(i), Some(j)) if i < j => { + cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i, + separator_index + 1) + } + // Potential *single* line comment. + (_, Some(j)) if j > separator_index => j + 1, + _ => post_snippet.len(), } } - - // Cleanup post-comment: strip separators and whitespace. - self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32); - let post_snippet = post_snippet[..comment_end].trim(); - - let post_snippet_trimmed = if post_snippet.starts_with(',') { - post_snippet[1..].trim_matches(white_space) - } else if post_snippet.ends_with(',') { - post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space) - } else { + None => { post_snippet - }; - - let post_comment = if !post_snippet_trimmed.is_empty() { - Some(post_snippet_trimmed.to_owned()) - } else { - None - }; - - ListItem { - pre_comment: pre_comment, - item: (self.get_item_string)(&item), - post_comment: post_comment, - new_lines: new_lines, + .find_uncommented(self.terminator) + .unwrap_or(post_snippet.len()) } - }) + }; + + if !post_snippet.is_empty() && comment_end > 0 { + // Account for extra whitespace between items. This is fiddly + // because of the way we divide pre- and post- comments. + + // Everything from the separator to the next item. + let test_snippet = &post_snippet[comment_end - 1..]; + let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len()); + // From the end of the first line of comments. + let test_snippet = &test_snippet[first_newline..]; + let first = test_snippet + .find(|c: char| !c.is_whitespace()) + .unwrap_or(test_snippet.len()); + // From the end of the first line of comments to the next non-whitespace char. + let test_snippet = &test_snippet[..first]; + + if test_snippet.chars().filter(|c| c == &'\n').count() > 1 { + // There were multiple line breaks which got trimmed to nothing. + new_lines = true; + } + } + + // Cleanup post-comment: strip separators and whitespace. + self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32); + let post_snippet = post_snippet[..comment_end].trim(); + + let post_snippet_trimmed = if post_snippet.starts_with(',') { + post_snippet[1..].trim_matches(white_space) + } else if post_snippet.ends_with(',') { + post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space) + } else { + post_snippet + }; + + let post_comment = if !post_snippet_trimmed.is_empty() { + Some(post_snippet_trimmed.to_owned()) + } else { + None + }; + + ListItem { + pre_comment: pre_comment, + item: (self.get_item_string)(&item), + post_comment: post_comment, + new_lines: new_lines, + } + }) } } diff --git a/src/macros.rs b/src/macros.rs index 8f998e42035..70924b90d23 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -159,15 +159,13 @@ pub fn rewrite_macro(mac: &ast::Mac, // Format macro invocation as array literal. let extra_offset = macro_name.len(); let shape = try_opt!(shape.shrink_left(extra_offset)); - let rewrite = try_opt!(rewrite_array(expr_vec.iter().map(|x| &**x), - mk_sp(context - .codemap - .span_after(mac.span, - original_style - .opener()), - mac.span.hi - BytePos(1)), - context, - shape)); + let rewrite = + try_opt!(rewrite_array(expr_vec.iter().map(|x| &**x), + mk_sp(context.codemap.span_after(mac.span, + original_style.opener()), + mac.span.hi - BytePos(1)), + context, + shape)); Some(format!("{}{}", macro_name, rewrite)) } diff --git a/src/missed_spans.rs b/src/missed_spans.rs index 843556b9b38..e83cc4d462d 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -158,13 +158,12 @@ impl<'a> FmtVisitor<'a> { self.config.max_width - self.block_indent.width()); - self.buffer - .push_str(&rewrite_comment(subslice, - false, - Shape::legacy(comment_width, - self.block_indent), - self.config) - .unwrap()); + self.buffer.push_str(&rewrite_comment(subslice, + false, + Shape::legacy(comment_width, + self.block_indent), + self.config) + .unwrap()); last_wspace = None; line_start = offset + subslice.len(); diff --git a/src/patterns.rs b/src/patterns.rs index 75175cc7667..a1628888622 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -39,11 +39,10 @@ impl Rewrite for Pat { let sub_pat = match *sub_pat { Some(ref p) => { // 3 - ` @ `. - let width = try_opt!(shape - .width - .checked_sub(prefix.len() + mut_infix.len() + - id_str.len() + - 3)); + let width = try_opt!(shape.width.checked_sub(prefix.len() + + mut_infix.len() + + id_str.len() + + 3)); format!(" @ {}", try_opt!(p.rewrite(context, Shape::legacy(width, shape.indent)))) } @@ -328,13 +327,11 @@ fn rewrite_tuple_pat(pats: &[ptr::P], fn count_wildcard_suffix_len(items: &[ListItem]) -> usize { let mut suffix_len = 0; - for item in items - .iter() - .rev() - .take_while(|i| match i.item { - Some(ref internal_string) if internal_string == "_" => true, - _ => false, - }) { + for item in items.iter().rev().take_while(|i| match i.item { + Some(ref internal_string) if internal_string == + "_" => true, + _ => false, + }) { suffix_len += 1; if item.pre_comment.is_some() || item.post_comment.is_some() { diff --git a/src/types.rs b/src/types.rs index 754db673013..5cb0fc3f49d 100644 --- a/src/types.rs +++ b/src/types.rs @@ -163,11 +163,10 @@ impl<'a> Rewrite for SegmentParam<'a> { TypeDensity::Compressed => format!("{}=", binding.ident), }; let budget = try_opt!(shape.width.checked_sub(result.len())); - let rewrite = - try_opt!(binding - .ty - .rewrite(context, - Shape::legacy(budget, shape.indent + result.len()))); + let rewrite = try_opt!(binding.ty.rewrite(context, + Shape::legacy(budget, + shape.indent + + result.len()))); result.push_str(&rewrite); Some(result) } @@ -563,10 +562,10 @@ impl Rewrite for ast::PolyTraitRef { // 6 is "for<> ".len() let extra_offset = lifetime_str.len() + 6; let max_path_width = try_opt!(shape.width.checked_sub(extra_offset)); - let path_str = try_opt!(self.trait_ref - .rewrite(context, - Shape::legacy(max_path_width, - shape.indent + extra_offset))); + let path_str = try_opt!(self.trait_ref.rewrite(context, + Shape::legacy(max_path_width, + shape.indent + + extra_offset))); Some(if context.config.spaces_within_angle_brackets && lifetime_str.len() > 0 { format!("for< {} > {}", lifetime_str, path_str) @@ -613,22 +612,20 @@ impl Rewrite for ast::Ty { format!("&{} {}{}", lt_str, mut_str, - try_opt!(mt.ty - .rewrite(context, - Shape::legacy(budget, - shape.indent + 2 + - mut_len + - lt_len)))) + try_opt!(mt.ty.rewrite(context, + Shape::legacy(budget, + shape.indent + 2 + + mut_len + + lt_len)))) } None => { let budget = try_opt!(shape.width.checked_sub(1 + mut_len)); format!("&{}{}", mut_str, - try_opt!(mt.ty - .rewrite(context, - Shape::legacy(budget, - shape.indent + 1 + - mut_len)))) + try_opt!(mt.ty.rewrite(context, + Shape::legacy(budget, + shape.indent + 1 + + mut_len)))) } }) } diff --git a/tests/system.rs b/tests/system.rs index 48531d321c8..66f7fa19cfb 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -303,21 +303,18 @@ fn read_significant_comments(file_name: &str) -> HashMap { .map(|line| line.expect("Failed getting line")) .take_while(|line| line_regex.is_match(&line)) .filter_map(|line| { - regex - .captures_iter(&line) - .next() - .map(|capture| { - (capture - .get(1) - .expect("Couldn't unwrap capture") - .as_str() - .to_owned(), - capture - .get(2) - .expect("Couldn't unwrap capture") - .as_str() - .to_owned()) - }) + regex.captures_iter(&line).next().map(|capture| { + (capture + .get(1) + .expect("Couldn't unwrap capture") + .as_str() + .to_owned(), + capture + .get(2) + .expect("Couldn't unwrap capture") + .as_str() + .to_owned()) + }) }) .collect() } diff --git a/tests/target/tuple.rs b/tests/target/tuple.rs index b7cbf82f411..2d3b52da72a 100644 --- a/tests/target/tuple.rs +++ b/tests/target/tuple.rs @@ -29,14 +29,13 @@ fn b() { } fn issue550() { - self.visitor - .visit_volume(self.level.sector_id(sector), - (floor_y, - if is_sky_flat(ceil_tex) { - from_wad_height(self.height_range.1) - } else { - ceil_y - })); + self.visitor.visit_volume(self.level.sector_id(sector), + (floor_y, + if is_sky_flat(ceil_tex) { + from_wad_height(self.height_range.1) + } else { + ceil_y + })); } fn issue775() {