From 01c1f99e3991caddabf9e90e6c5d3db6edf87f30 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Sat, 14 Apr 2018 10:15:39 +1200 Subject: [PATCH] Fallout from removing TupField --- src/chains.rs | 22 +++++++++++++++------- src/expr.rs | 2 -- src/utils.rs | 1 - 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index 15ecd6034f31..50e8ba6cdefa 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -393,7 +393,6 @@ fn pop_expr_chain(expr: &ast::Expr, context: &RewriteContext) -> Option { Some(convert_try(&expressions[0], context)) } - ast::ExprKind::TupField(ref subexpr, _) | ast::ExprKind::Field(ref subexpr, _) | ast::ExprKind::Try(ref subexpr) => Some(convert_try(subexpr, context)), _ => None, @@ -440,19 +439,28 @@ fn rewrite_chain_subexpr( }; rewrite_method_call(segment.ident, types, expressions, span, context, shape) } - ast::ExprKind::Field(_, ref field) => rewrite_element(format!(".{}", field.name)), - ast::ExprKind::TupField(ref expr, ref field) => { - let space = match expr.node { - ast::ExprKind::TupField(..) => " ", - _ => "", + ast::ExprKind::Field(ref nested, ref field) => { + let space = if is_tup_field_access(expr) && is_tup_field_access(nested) { + " " + } else { + "" }; - rewrite_element(format!("{}.{}", space, field.node)) + rewrite_element(format!("{}.{}", space, field.name)) } ast::ExprKind::Try(_) => rewrite_element(String::from("?")), _ => unreachable!(), } } +fn is_tup_field_access(expr: &ast::Expr) -> bool { + match expr.node { + ast::ExprKind::Field(_, ref field) => { + field.name.to_string().chars().all(|c| c.is_digit(10)) + } + _ => false, + } +} + // Determines if we can continue formatting a given expression on the same line. fn is_continuable(expr: &ast::Expr) -> bool { match expr.node { diff --git a/src/expr.rs b/src/expr.rs index af51ae2ebc16..7c4c16178a7e 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -178,7 +178,6 @@ pub fn format_expr( } ast::ExprKind::Try(..) | ast::ExprKind::Field(..) - | ast::ExprKind::TupField(..) | ast::ExprKind::MethodCall(..) => rewrite_chain(expr, context, shape), ast::ExprKind::Mac(ref mac) => { rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| { @@ -1349,7 +1348,6 @@ fn is_simple_expr(expr: &ast::Expr) -> bool { | ast::ExprKind::Cast(ref expr, _) | ast::ExprKind::Field(ref expr, _) | ast::ExprKind::Try(ref expr) - | ast::ExprKind::TupField(ref expr, _) | ast::ExprKind::Unary(_, ref expr) => is_simple_expr(expr), ast::ExprKind::Index(ref lhs, ref rhs) | ast::ExprKind::Repeat(ref lhs, ref rhs) => { is_simple_expr(lhs) && is_simple_expr(rhs) diff --git a/src/utils.rs b/src/utils.rs index dae57882a35d..00d6cf8ed08d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -365,7 +365,6 @@ pub fn left_most_sub_expr(e: &ast::Expr) -> &ast::Expr { | ast::ExprKind::Assign(ref e, _) | ast::ExprKind::AssignOp(_, ref e, _) | ast::ExprKind::Field(ref e, _) - | ast::ExprKind::TupField(ref e, _) | ast::ExprKind::Index(ref e, _) | ast::ExprKind::Range(Some(ref e), _, _) | ast::ExprKind::Try(ref e) => left_most_sub_expr(e),