diff --git a/src/bin/cargo-fmt.rs b/src/bin/cargo-fmt.rs index 9a7c9680a5c..3f7d11c0a97 100644 --- a/src/bin/cargo-fmt.rs +++ b/src/bin/cargo-fmt.rs @@ -90,13 +90,11 @@ fn execute() -> i32 { print_usage(&opts, &e.to_string()); failure } - Ok(status) => { - if status.success() { - success - } else { - status.code().unwrap_or(failure) - } - } + Ok(status) => if status.success() { + success + } else { + status.code().unwrap_or(failure) + }, } } @@ -324,12 +322,10 @@ fn format_files( .args(fmt_args) .spawn() .map_err(|e| match e.kind() { - std::io::ErrorKind::NotFound => { - std::io::Error::new( - std::io::ErrorKind::Other, - "Could not run rustfmt, please make sure it is in your PATH.", - ) - } + std::io::ErrorKind::NotFound => std::io::Error::new( + std::io::ErrorKind::Other, + "Could not run rustfmt, please make sure it is in your PATH.", + ), _ => e, })?; command.wait() diff --git a/src/chains.rs b/src/chains.rs index 4e606369eb9..1660e113dcd 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -444,12 +444,10 @@ fn rewrite_method_call_with_overflow( None => return false, }; let types = match segment.parameters { - Some(ref params) => { - match **params { - ast::PathParameters::AngleBracketed(ref data) => &data.types[..], - _ => &[], - } - } + Some(ref params) => match **params { + ast::PathParameters::AngleBracketed(ref data) => &data.types[..], + _ => &[], + }, _ => &[], }; let mut last_rewrite = rewrite_method_call( @@ -516,12 +514,10 @@ fn rewrite_chain_subexpr( match expr.node { ast::ExprKind::MethodCall(ref segment, ref expressions) => { let types = match segment.parameters { - Some(ref params) => { - match **params { - ast::PathParameters::AngleBracketed(ref data) => &data.types[..], - _ => &[], - } - } + Some(ref params) => match **params { + ast::PathParameters::AngleBracketed(ref data) => &data.types[..], + _ => &[], + }, _ => &[], }; rewrite_method_call(segment.identifier, types, expressions, span, context, shape) diff --git a/src/comment.rs b/src/comment.rs index 123df4927f4..df97c2bfef3 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -342,14 +342,12 @@ impl FindUncommented for str { None => { return Some(i - pat.len()); } - Some(c) => { - match kind { - FullCodeCharKind::Normal if b == c => {} - _ => { - needle_iter = pat.chars(); - } + Some(c) => match kind { + FullCodeCharKind::Normal if b == c => {} + _ => { + needle_iter = pat.chars(); } - } + }, } } @@ -494,42 +492,34 @@ where let item = try_opt!(self.base.next()); let chr = item.get_char(); self.status = match self.status { - CharClassesStatus::LitString => { - match chr { - '"' => CharClassesStatus::Normal, - '\\' => CharClassesStatus::LitStringEscape, - _ => CharClassesStatus::LitString, - } - } + CharClassesStatus::LitString => match chr { + '"' => CharClassesStatus::Normal, + '\\' => CharClassesStatus::LitStringEscape, + _ => CharClassesStatus::LitString, + }, CharClassesStatus::LitStringEscape => CharClassesStatus::LitString, - CharClassesStatus::LitChar => { - match chr { - '\\' => CharClassesStatus::LitCharEscape, - '\'' => CharClassesStatus::Normal, - _ => CharClassesStatus::LitChar, - } - } + CharClassesStatus::LitChar => match chr { + '\\' => CharClassesStatus::LitCharEscape, + '\'' => CharClassesStatus::Normal, + _ => CharClassesStatus::LitChar, + }, CharClassesStatus::LitCharEscape => CharClassesStatus::LitChar, - CharClassesStatus::Normal => { - match chr { - '"' => CharClassesStatus::LitString, - '\'' => CharClassesStatus::LitChar, - '/' => { - match self.base.peek() { - Some(next) if next.get_char() == '*' => { - self.status = CharClassesStatus::BlockCommentOpening(1); - return Some((FullCodeCharKind::StartComment, item)); - } - Some(next) if next.get_char() == '/' => { - self.status = CharClassesStatus::LineComment; - return Some((FullCodeCharKind::StartComment, item)); - } - _ => CharClassesStatus::Normal, - } + CharClassesStatus::Normal => match chr { + '"' => CharClassesStatus::LitString, + '\'' => CharClassesStatus::LitChar, + '/' => match self.base.peek() { + Some(next) if next.get_char() == '*' => { + self.status = CharClassesStatus::BlockCommentOpening(1); + return Some((FullCodeCharKind::StartComment, item)); + } + Some(next) if next.get_char() == '/' => { + self.status = CharClassesStatus::LineComment; + return Some((FullCodeCharKind::StartComment, item)); } _ => CharClassesStatus::Normal, - } - } + }, + _ => CharClassesStatus::Normal, + }, CharClassesStatus::BlockComment(deepness) => { assert!(deepness != 0); self.status = match self.base.peek() { @@ -558,18 +548,16 @@ where return Some((FullCodeCharKind::InComment, item)); } } - CharClassesStatus::LineComment => { - match chr { - '\n' => { - self.status = CharClassesStatus::Normal; - return Some((FullCodeCharKind::EndComment, item)); - } - _ => { - self.status = CharClassesStatus::LineComment; - return Some((FullCodeCharKind::InComment, item)); - } + CharClassesStatus::LineComment => match chr { + '\n' => { + self.status = CharClassesStatus::Normal; + return Some((FullCodeCharKind::EndComment, item)); } - } + _ => { + self.status = CharClassesStatus::LineComment; + return Some((FullCodeCharKind::InComment, item)); + } + }, }; Some((FullCodeCharKind::Normal, item)) } diff --git a/src/config.rs b/src/config.rs index fa72e2b85d5..510cdef6f1b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -482,11 +482,9 @@ pub fn get_toml_path(dir: &Path) -> Result, Error> { Ok(ref md) if md.is_file() => return Ok(Some(config_file)), // Return the error if it's something other than `NotFound`; otherwise we didn't // find the project file yet, and continue searching. - Err(e) => { - if e.kind() != ErrorKind::NotFound { - return Err(e); - } - } + Err(e) => if e.kind() != ErrorKind::NotFound { + return Err(e); + }, _ => {} } } diff --git a/src/expr.rs b/src/expr.rs index d60377df65a..9adb601c9cb 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -88,29 +88,23 @@ pub fn format_expr( } } let expr_rw = match expr.node { - ast::ExprKind::Array(ref expr_vec) => { - rewrite_array( - expr_vec.iter().map(|e| &**e), - mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi), - context, - shape, - false, - ) - } - ast::ExprKind::Lit(ref l) => { - match l.node { - ast::LitKind::Str(_, ast::StrStyle::Cooked) => { - rewrite_string_lit(context, l.span, shape) - } - _ => { - wrap_str( - context.snippet(expr.span), - context.config.max_width(), - shape, - ) - } + ast::ExprKind::Array(ref expr_vec) => rewrite_array( + expr_vec.iter().map(|e| &**e), + mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi), + context, + shape, + false, + ), + ast::ExprKind::Lit(ref l) => match l.node { + ast::LitKind::Str(_, ast::StrStyle::Cooked) => { + rewrite_string_lit(context, l.span, shape) } - } + _ => wrap_str( + context.snippet(expr.span), + context.config.max_width(), + shape, + ), + }, ast::ExprKind::Call(ref callee, ref args) => { let inner_span = mk_sp(callee.span.hi, expr.span.hi); rewrite_call_with_binary_search( @@ -135,33 +129,27 @@ pub fn format_expr( ) } ast::ExprKind::Unary(ref op, ref subexpr) => rewrite_unary_op(context, op, subexpr, shape), - ast::ExprKind::Struct(ref path, ref fields, ref base) => { - rewrite_struct_lit( - context, - path, - fields, - base.as_ref().map(|e| &**e), - expr.span, - shape, - ) - } - ast::ExprKind::Tup(ref items) => { - rewrite_tuple( - context, - &items.iter().map(|x| &**x).collect::>()[..], - expr.span, - shape, - ) - } + ast::ExprKind::Struct(ref path, ref fields, ref base) => rewrite_struct_lit( + context, + path, + fields, + base.as_ref().map(|e| &**e), + expr.span, + shape, + ), + ast::ExprKind::Tup(ref items) => rewrite_tuple( + context, + &items.iter().map(|x| &**x).collect::>()[..], + expr.span, + shape, + ), ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) | ast::ExprKind::ForLoop(..) | ast::ExprKind::Loop(..) | ast::ExprKind::While(..) | - ast::ExprKind::WhileLet(..) => { - to_control_flow(expr, expr_type) - .and_then(|control_flow| control_flow.rewrite(context, shape)) - } + ast::ExprKind::WhileLet(..) => to_control_flow(expr, expr_type) + .and_then(|control_flow| control_flow.rewrite(context, shape)), ast::ExprKind::Block(ref block) => { match expr_type { ExprType::Statement => { @@ -271,14 +259,12 @@ pub fn format_expr( fn needs_space_before_range(context: &RewriteContext, lhs: &ast::Expr) -> bool { match lhs.node { - ast::ExprKind::Lit(ref lit) => { - match lit.node { - ast::LitKind::FloatUnsuffixed(..) => { - context.snippet(lit.span).ends_with('.') - } - _ => false, + ast::ExprKind::Lit(ref lit) => match lit.node { + ast::LitKind::FloatUnsuffixed(..) => { + context.snippet(lit.span).ends_with('.') } - } + _ => false, + }, _ => false, } } @@ -315,13 +301,11 @@ pub fn format_expr( } // We do not format these expressions yet, but they should still // satisfy our width restrictions. - ast::ExprKind::InPlace(..) | ast::ExprKind::InlineAsm(..) => { - wrap_str( - context.snippet(expr.span), - context.config.max_width(), - shape, - ) - } + ast::ExprKind::InPlace(..) | ast::ExprKind::InlineAsm(..) => wrap_str( + context.snippet(expr.span), + context.config.max_width(), + shape, + ), ast::ExprKind::Catch(ref block) => { if let rewrite @ Some(_) = rewrite_single_line_block(context, "do catch ", block, shape) @@ -338,14 +322,12 @@ pub fn format_expr( } }; match (attr_rw, expr_rw) { - (Some(attr_str), Some(expr_str)) => { - recover_comment_removed( - combine_attr_and_expr(context, shape, &attr_str, &expr_str), - expr.span, - context, - shape, - ) - } + (Some(attr_str), Some(expr_str)) => recover_comment_removed( + combine_attr_and_expr(context, shape, &attr_str, &expr_str), + expr.span, + context, + shape, + ), _ => None, } } @@ -460,22 +442,18 @@ where }; let mut nested_shape = match context.config.array_layout() { - IndentStyle::Block => { - try_opt!( - shape - .block() - .block_indent(context.config.tab_spaces()) - .with_max_width(context.config) - .sub_width(1) - ) - } - IndentStyle::Visual => { - try_opt!( - shape - .visual_indent(bracket_size) - .sub_width(bracket_size * 2) - ) - } + IndentStyle::Block => try_opt!( + shape + .block() + .block_indent(context.config.tab_spaces()) + .with_max_width(context.config) + .sub_width(1) + ), + IndentStyle::Visual => try_opt!( + shape + .visual_indent(bracket_size) + .sub_width(bracket_size * 2) + ), }; let items = itemize_list( @@ -513,17 +491,15 @@ where None => DefinitiveListTactic::Vertical, } } - IndentStyle::Visual => { - if has_long_item || items.iter().any(ListItem::is_multiline) { - definitive_tactic( - &items, - ListTactic::LimitedHorizontalVertical(context.config.array_width()), - nested_shape.width, - ) - } else { - DefinitiveListTactic::Mixed - } - } + IndentStyle::Visual => if has_long_item || items.iter().any(ListItem::is_multiline) { + definitive_tactic( + &items, + ListTactic::LimitedHorizontalVertical(context.config.array_width()), + nested_shape.width, + ) + } else { + DefinitiveListTactic::Mixed + }, }; if context.config.array_horizontal_layout_threshold() > 0 && items.len() > context.config.array_horizontal_layout_threshold() @@ -965,15 +941,13 @@ fn rewrite_cond(context: &RewriteContext, expr: &ast::Expr, shape: Shape) -> Opt ast::ExprKind::Block(ref block) if block.stmts.len() == 1 => { stmt_expr(&block.stmts[0]).and_then(|e| rewrite_cond(context, e, shape)) } - _ => { - to_control_flow(expr, ExprType::SubExpression).and_then(|control_flow| { - let alt_block_sep = - String::from("\n") + &shape.indent.block_only().to_string(context.config); - control_flow - .rewrite_cond(context, shape, &alt_block_sep) - .and_then(|rw| Some(rw.0)) - }) - } + _ => to_control_flow(expr, ExprType::SubExpression).and_then(|control_flow| { + let alt_block_sep = + String::from("\n") + &shape.indent.block_only().to_string(context.config); + control_flow + .rewrite_cond(context, shape, &alt_block_sep) + .and_then(|rw| Some(rw.0)) + }), } } @@ -996,17 +970,15 @@ struct ControlFlow<'a> { fn to_control_flow<'a>(expr: &'a ast::Expr, expr_type: ExprType) -> Option> { match expr.node { - ast::ExprKind::If(ref cond, ref if_block, ref else_block) => { - Some(ControlFlow::new_if( - cond, - None, - if_block, - else_block.as_ref().map(|e| &**e), - expr_type == ExprType::SubExpression, - false, - expr.span, - )) - } + ast::ExprKind::If(ref cond, ref if_block, ref else_block) => Some(ControlFlow::new_if( + cond, + None, + if_block, + else_block.as_ref().map(|e| &**e), + expr_type == ExprType::SubExpression, + false, + expr.span, + )), ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref else_block) => { Some(ControlFlow::new_if( cond, @@ -1021,21 +993,15 @@ fn to_control_flow<'a>(expr: &'a ast::Expr, expr_type: ExprType) -> Option { Some(ControlFlow::new_for(pat, cond, block, label, expr.span)) } - ast::ExprKind::Loop(ref block, label) => Some( - ControlFlow::new_loop(block, label, expr.span), - ), - ast::ExprKind::While(ref cond, ref block, label) => Some( - ControlFlow::new_while(None, cond, block, label, expr.span), - ), - ast::ExprKind::WhileLet(ref pat, ref cond, ref block, label) => { - Some(ControlFlow::new_while( - Some(pat), - cond, - block, - label, - expr.span, - )) + ast::ExprKind::Loop(ref block, label) => { + Some(ControlFlow::new_loop(block, label, expr.span)) + } + ast::ExprKind::While(ref cond, ref block, label) => { + Some(ControlFlow::new_while(None, cond, block, label, expr.span)) } + ast::ExprKind::WhileLet(ref pat, ref cond, ref block, label) => Some( + ControlFlow::new_while(Some(pat), cond, block, label, expr.span), + ), _ => None, } } @@ -2502,26 +2468,22 @@ fn rewrite_index( let index_shape = try_opt!(index_shape.sub_width(rbr.len() + rhs_overhead)); let new_index_rw = index.rewrite(context, index_shape); match (orig_index_rw, new_index_rw) { - (_, Some(ref new_index_str)) if !new_index_str.contains('\n') => { - Some(format!( - "{}\n{}{}{}{}", - expr_str, - indent.to_string(&context.config), - lbr, - new_index_str, - rbr - )) - } - (None, Some(ref new_index_str)) => { - Some(format!( - "{}\n{}{}{}{}", - expr_str, - indent.to_string(&context.config), - lbr, - new_index_str, - rbr - )) - } + (_, Some(ref new_index_str)) if !new_index_str.contains('\n') => Some(format!( + "{}\n{}{}{}{}", + expr_str, + indent.to_string(&context.config), + lbr, + new_index_str, + rbr + )), + (None, Some(ref new_index_str)) => Some(format!( + "{}\n{}{}{}{}", + expr_str, + indent.to_string(&context.config), + lbr, + new_index_str, + rbr + )), (Some(ref index_str), _) => Some(format!("{}{}{}{}", expr_str, lbr, index_str, rbr)), _ => None, } diff --git a/src/imports.rs b/src/imports.rs index 8ded75a9576..d284b235c5e 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -60,12 +60,10 @@ fn compare_path_list_items(a: &ast::PathListItem, b: &ast::PathListItem) -> Orde }; if name_ordering == Ordering::Equal { match a.node.rename { - Some(a_rename) => { - match b.node.rename { - Some(b_rename) => a_rename.name.as_str().cmp(&b_rename.name.as_str()), - None => Ordering::Greater, - } - } + Some(a_rename) => match b.node.rename { + Some(b_rename) => a_rename.name.as_str().cmp(&b_rename.name.as_str()), + None => Ordering::Greater, + }, None => Ordering::Less, } } else { @@ -159,9 +157,9 @@ impl Rewrite for ast::ViewPath { // Returns an empty string when the ViewPath is empty (like foo::bar::{}) fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { match self.node { - ast::ViewPath_::ViewPathList(_, ref path_list) if path_list.is_empty() => Some( - String::new(), - ), + ast::ViewPath_::ViewPathList(_, ref path_list) if path_list.is_empty() => { + Some(String::new()) + } ast::ViewPath_::ViewPathList(ref path, ref path_list) => { rewrite_use_list(shape, path, path_list, self.span, context) } diff --git a/src/issues.rs b/src/issues.rs index 0211723e930..432fb797a6e 100644 --- a/src/issues.rs +++ b/src/issues.rs @@ -189,25 +189,19 @@ impl BadIssueSeeker { } match part { - NumberPart::OpenParen => { - if c != '(' { - return IssueClassification::Bad(issue); - } else { - part = NumberPart::Pound; - } - } - NumberPart::Pound => { - if c == '#' { - part = NumberPart::Number; - } - } - NumberPart::Number => { - if c >= '0' && c <= '9' { - part = NumberPart::CloseParen; - } else { - return IssueClassification::Bad(issue); - } - } + NumberPart::OpenParen => if c != '(' { + return IssueClassification::Bad(issue); + } else { + part = NumberPart::Pound; + }, + NumberPart::Pound => if c == '#' { + part = NumberPart::Number; + }, + NumberPart::Number => if c >= '0' && c <= '9' { + part = NumberPart::CloseParen; + } else { + return IssueClassification::Bad(issue); + }, NumberPart::CloseParen => {} } diff --git a/src/items.rs b/src/items.rs index a5e35e144fb..7e79e0a0b45 100644 --- a/src/items.rs +++ b/src/items.rs @@ -362,12 +362,10 @@ impl<'a> FmtVisitor<'a> { ).map(|s| s + suffix) .or_else(|| Some(self.snippet(e.span))) } - None => { - stmt.rewrite( - &self.get_context(), - Shape::indented(self.block_indent, self.config), - ) - } + None => stmt.rewrite( + &self.get_context(), + Shape::indented(self.block_indent, self.config), + ), } } else { None @@ -418,13 +416,11 @@ impl<'a> FmtVisitor<'a> { let variant_list = self.format_variant_list(enum_def, body_start, span.hi - BytePos(1)); match variant_list { Some(ref body_str) => self.buffer.push_str(body_str), - None => { - if contains_comment(&enum_snippet[brace_pos..]) { - self.format_missing_no_indent(span.hi - BytePos(1)) - } else { - self.format_missing(span.hi - BytePos(1)) - } - } + None => if contains_comment(&enum_snippet[brace_pos..]) { + self.format_missing_no_indent(span.hi - BytePos(1)) + } else { + self.format_missing(span.hi - BytePos(1)) + }, } self.block_indent = self.block_indent.block_unindent(self.config); @@ -620,14 +616,12 @@ pub fn format_impl( result.push_str(&offset.to_string(context.config)); } BraceStyle::PreferSameLine => result.push(' '), - BraceStyle::SameLineWhere => { - if !where_clause_str.is_empty() { - result.push('\n'); - result.push_str(&offset.to_string(context.config)); - } else { - result.push(' '); - } - } + BraceStyle::SameLineWhere => if !where_clause_str.is_empty() { + result.push('\n'); + result.push_str(&offset.to_string(context.config)); + } else { + result.push(' '); + }, } result.push('{'); @@ -876,31 +870,27 @@ pub fn format_struct( ) -> Option { match *struct_def { ast::VariantData::Unit(..) => Some(format_unit_struct(item_name, ident, vis)), - ast::VariantData::Tuple(ref fields, _) => { - format_tuple_struct( - context, - item_name, - ident, - vis, - fields, - generics, - span, - offset, - ) - } - ast::VariantData::Struct(ref fields, _) => { - format_struct_struct( - context, - item_name, - ident, - vis, - fields, - generics, - span, - offset, - one_line_width, - ) - } + ast::VariantData::Tuple(ref fields, _) => format_tuple_struct( + context, + item_name, + ident, + vis, + fields, + generics, + span, + offset, + ), + ast::VariantData::Struct(ref fields, _) => format_struct_struct( + context, + item_name, + ident, + vis, + fields, + generics, + span, + offset, + one_line_width, + ), } } @@ -1003,16 +993,14 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) result.push_str(&offset.to_string(context.config)); } BraceStyle::PreferSameLine => result.push(' '), - BraceStyle::SameLineWhere => { - if !where_clause_str.is_empty() && - (!trait_items.is_empty() || result.contains('\n')) - { - result.push('\n'); - result.push_str(&offset.to_string(context.config)); - } else { - result.push(' '); - } - } + BraceStyle::SameLineWhere => if !where_clause_str.is_empty() && + (!trait_items.is_empty() || result.contains('\n')) + { + result.push('\n'); + result.push_str(&offset.to_string(context.config)); + } else { + result.push(' '); + }, } result.push('{'); @@ -1072,19 +1060,17 @@ pub fn format_struct_struct( let body_lo = context.codemap.span_after(span, "{"); let generics_str = match generics { - Some(g) => { - try_opt!(format_generics( - context, - g, - "{", - "{", - context.config.item_brace_style(), - fields.is_empty(), - offset, - mk_sp(span.lo, body_lo), - last_line_width(&result), - )) - } + Some(g) => try_opt!(format_generics( + context, + g, + "{", + "{", + context.config.item_brace_style(), + fields.is_empty(), + offset, + mk_sp(span.lo, body_lo), + last_line_width(&result), + )), None => { // 3 = ` {}`, 2 = ` {`. let overhead = if fields.is_empty() { 3 } else { 2 }; @@ -1416,16 +1402,14 @@ pub fn rewrite_struct_field_prefix( let type_annotation_spacing = type_annotation_spacing(context.config); Some(match field.ident { - Some(name) => { - format!( - "{}{}{}{}{}:", - attr_str, - missing_comment, - vis, - name, - type_annotation_spacing.0 - ) - } + Some(name) => format!( + "{}{}{}{}{}:", + attr_str, + missing_comment, + vis, + name, + type_annotation_spacing.0 + ), None => format!("{}{}{}", attr_str, missing_comment, vis), }) } @@ -1492,19 +1476,15 @@ pub fn rewrite_struct_field( match ty_rewritten { // If we start from the next line and type fits in a single line, then do so. - Some(ref ty) => { - match rewrite_type_in_next_line() { - Some(ref new_ty) if !new_ty.contains('\n') => { - Some(format!( - "{}\n{}{}", - prefix, - type_offset.to_string(&context.config), - &new_ty - )) - } - _ => Some(prefix + &ty), - } - } + Some(ref ty) => match rewrite_type_in_next_line() { + Some(ref new_ty) if !new_ty.contains('\n') => Some(format!( + "{}\n{}{}", + prefix, + type_offset.to_string(&context.config), + &new_ty + )), + _ => Some(prefix + &ty), + }, _ => { let ty = try_opt!(rewrite_type_in_next_line()); Some(format!( @@ -2221,20 +2201,16 @@ fn rewrite_args( .map_or(false, |s| s.trim().starts_with("//")); let (indent, trailing_comma, end_with_newline) = match context.config.fn_args_layout() { - IndentStyle::Block if fits_in_one_line => { - ( - indent.block_indent(context.config), - SeparatorTactic::Never, - true, - ) - } - IndentStyle::Block => { - ( - indent.block_indent(context.config), - context.config.trailing_comma(), - true, - ) - } + IndentStyle::Block if fits_in_one_line => ( + indent.block_indent(context.config), + SeparatorTactic::Never, + true, + ), + IndentStyle::Block => ( + indent.block_indent(context.config), + context.config.trailing_comma(), + true, + ), IndentStyle::Visual if last_line_ends_with_comment => { (arg_indent, context.config.trailing_comma(), true) } diff --git a/src/lib.rs b/src/lib.rs index 899d168cee4..030d18c3000 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -442,14 +442,12 @@ pub enum ErrorKind { impl fmt::Display for ErrorKind { fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { match *self { - ErrorKind::LineOverflow(found, maximum) => { - write!( - fmt, - "line exceeded maximum length (maximum: {}, found: {})", - maximum, - found - ) - } + ErrorKind::LineOverflow(found, maximum) => write!( + fmt, + "line exceeded maximum length (maximum: {}, found: {})", + maximum, + found + ), ErrorKind::TrailingWhitespace => write!(fmt, "left behind trailing whitespace"), ErrorKind::BadIssue(issue) => write!(fmt, "found {}", issue), } diff --git a/src/lists.rs b/src/lists.rs index 4f762ea1f37..4bd7d76483a 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -440,29 +440,23 @@ where // 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, - ) - } + (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, - ) - } + (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(), } } - None => { - post_snippet - .find_uncommented(self.terminator) - .unwrap_or(post_snippet.len()) - } + None => post_snippet + .find_uncommented(self.terminator) + .unwrap_or(post_snippet.len()), }; if !post_snippet.is_empty() && comment_end > 0 { @@ -595,11 +589,9 @@ pub fn struct_lit_shape( suffix_width: usize, ) -> Option<(Option, Shape)> { let v_shape = match context.config.struct_lit_style() { - IndentStyle::Visual => { - try_opt!( - try_opt!(shape.visual_indent(0).shrink_left(prefix_width)).sub_width(suffix_width) - ) - } + IndentStyle::Visual => try_opt!( + try_opt!(shape.visual_indent(0).shrink_left(prefix_width)).sub_width(suffix_width) + ), IndentStyle::Block => { let shape = shape.block_indent(context.config.tab_spaces()); Shape { diff --git a/src/macros.rs b/src/macros.rs index 54a65087e12..3f4adf88120 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -80,13 +80,11 @@ pub fn rewrite_macro( let macro_name = match extra_ident { None => format!("{}!", mac.node.path), - Some(ident) => { - if ident == symbol::keywords::Invalid.ident() { - format!("{}!", mac.node.path) - } else { - format!("{}! {}", mac.node.path, ident) - } - } + Some(ident) => if ident == symbol::keywords::Invalid.ident() { + format!("{}!", mac.node.path) + } else { + format!("{}! {}", mac.node.path, ident) + }, }; let style = if FORCED_BRACKET_MACROS.contains(&¯o_name[..]) { diff --git a/src/patterns.rs b/src/patterns.rs index 2fce2b7ccad..bcc57f93189 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -54,23 +54,15 @@ impl Rewrite for Pat { let result = format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat); wrap_str(result, context.config.max_width(), shape) } - PatKind::Wild => { - if 1 <= shape.width { - Some("_".to_owned()) - } else { - None - } - } - PatKind::Range(ref lhs, ref rhs, ref end_kind) => { - match *end_kind { - RangeEnd::Included => { - rewrite_pair(&**lhs, &**rhs, "", "...", "", context, shape) - } - RangeEnd::Excluded => { - rewrite_pair(&**lhs, &**rhs, "", "..", "", context, shape) - } - } - } + PatKind::Wild => if 1 <= shape.width { + Some("_".to_owned()) + } else { + None + }, + PatKind::Range(ref lhs, ref rhs, ref end_kind) => match *end_kind { + RangeEnd::Included => rewrite_pair(&**lhs, &**rhs, "", "...", "", context, shape), + RangeEnd::Excluded => rewrite_pair(&**lhs, &**rhs, "", "..", "", context, shape), + }, PatKind::Ref(ref pat, mutability) => { let prefix = format!("&{}", format_mutability(mutability)); rewrite_unary_prefix(context, &prefix, &**pat, shape) @@ -121,13 +113,11 @@ impl Rewrite for Pat { rewrite_struct_pat(path, fields, elipses, self.span, context, shape) } // FIXME(#819) format pattern macros. - PatKind::Mac(..) => { - wrap_str( - context.snippet(self.span), - context.config.max_width(), - shape, - ) - } + PatKind::Mac(..) => wrap_str( + context.snippet(self.span), + context.config.max_width(), + shape, + ), } } } @@ -250,18 +240,16 @@ impl<'a> Spanned for TuplePatField<'a> { pub fn can_be_overflowed_pat(context: &RewriteContext, pat: &TuplePatField, len: usize) -> bool { match pat { - &TuplePatField::Pat(ref pat) => { - match pat.node { - ast::PatKind::Path(..) | ast::PatKind::Tuple(..) | ast::PatKind::Struct(..) => { - context.use_block_indent() && len == 1 - } - ast::PatKind::Ref(ref p, _) | ast::PatKind::Box(ref p) => { - can_be_overflowed_pat(context, &TuplePatField::Pat(p), len) - } - ast::PatKind::Lit(ref expr) => can_be_overflowed_expr(context, expr, len), - _ => false, + &TuplePatField::Pat(ref pat) => match pat.node { + ast::PatKind::Path(..) | ast::PatKind::Tuple(..) | ast::PatKind::Struct(..) => { + context.use_block_indent() && len == 1 } - } + ast::PatKind::Ref(ref p, _) | ast::PatKind::Box(ref p) => { + can_be_overflowed_pat(context, &TuplePatField::Pat(p), len) + } + ast::PatKind::Lit(ref expr) => can_be_overflowed_expr(context, expr, len), + _ => false, + }, &TuplePatField::Dotdot(..) => false, } } diff --git a/src/types.rs b/src/types.rs index 6fd070291af..edab375b02d 100644 --- a/src/types.rs +++ b/src/types.rs @@ -474,14 +474,12 @@ impl Rewrite for ast::WherePredicate { ref lifetime, ref bounds, .. - }) => { - try_opt!(rewrite_bounded_lifetime( - lifetime, - bounds.iter(), - context, - shape, - )) - } + }) => try_opt!(rewrite_bounded_lifetime( + lifetime, + bounds.iter(), + context, + shape, + )), ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { ref lhs_ty, ref rhs_ty, @@ -727,14 +725,12 @@ impl Rewrite for ast::Ty { format!("[{}]", ty_str) }) } - ast::TyKind::Tup(ref items) => { - rewrite_tuple( - context, - &items.iter().map(|x| &**x).collect::>()[..], - self.span, - shape, - ) - } + ast::TyKind::Tup(ref items) => rewrite_tuple( + context, + &items.iter().map(|x| &**x).collect::>()[..], + self.span, + shape, + ), ast::TyKind::Path(ref q_self, ref path) => { rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape) } @@ -744,21 +740,17 @@ impl Rewrite for ast::Ty { let rbr = if use_spaces { " ]" } else { "]" }; rewrite_pair(&**ty, &**repeats, lbr, "; ", rbr, context, shape) } - ast::TyKind::Infer => { - if shape.width >= 1 { - Some("_".to_owned()) - } else { - None - } - } + ast::TyKind::Infer => if shape.width >= 1 { + Some("_".to_owned()) + } else { + None + }, ast::TyKind::BareFn(ref bare_fn) => rewrite_bare_fn(bare_fn, self.span, context, shape), ast::TyKind::Never => Some(String::from("!")), ast::TyKind::Mac(..) => None, ast::TyKind::ImplicitSelf => Some(String::from("")), - ast::TyKind::ImplTrait(ref it) => { - it.rewrite(context, shape) - .map(|it_str| format!("impl {}", it_str)) - } + ast::TyKind::ImplTrait(ref it) => it.rewrite(context, shape) + .map(|it_str| format!("impl {}", it_str)), ast::TyKind::Err | ast::TyKind::Typeof(..) => unreachable!(), } } diff --git a/src/utils.rs b/src/utils.rs index 7873cd5d846..2e0dce52e91 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -25,11 +25,9 @@ use SKIP_ANNOTATION; pub fn extra_offset(text: &str, shape: Shape) -> usize { match text.rfind('\n') { // 1 for newline character - Some(idx) => { - text.len() - .checked_sub(idx + 1 + shape.used_width()) - .unwrap_or(0) - } + Some(idx) => text.len() + .checked_sub(idx + 1 + shape.used_width()) + .unwrap_or(0), None => text.len(), } } @@ -168,15 +166,13 @@ pub fn semicolon_for_expr(expr: &ast::Expr) -> bool { #[inline] pub fn semicolon_for_stmt(stmt: &ast::Stmt) -> bool { match stmt.node { - ast::StmtKind::Semi(ref expr) => { - match expr.node { - ast::ExprKind::While(..) | - ast::ExprKind::WhileLet(..) | - ast::ExprKind::Loop(..) | - ast::ExprKind::ForLoop(..) => false, - _ => true, - } - } + ast::StmtKind::Semi(ref expr) => match expr.node { + ast::ExprKind::While(..) | + ast::ExprKind::WhileLet(..) | + ast::ExprKind::Loop(..) | + ast::ExprKind::ForLoop(..) => false, + _ => true, + }, ast::StmtKind::Expr(..) => false, _ => true, } diff --git a/src/visitor.rs b/src/visitor.rs index 800f46374ec..a1258092e8d 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -289,12 +289,10 @@ impl<'a> FmtVisitor<'a> { assert!(!self.visit_attrs(&attrs)); } } - _ => { - if self.visit_attrs(&item.attrs) { - self.push_rewrite(item.span, None); - return; - } - } + _ => if self.visit_attrs(&item.attrs) { + self.push_rewrite(item.span, None); + return; + }, } match item.node {