From 183e3482e58d2cc107046386a816644c7b4f7b69 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Tue, 4 Jul 2017 20:21:49 +0900 Subject: [PATCH] Add a space before range if lhs ends with dot --- src/expr.rs | 16 ++++++++++++++++ tests/source/expr.rs | 4 ++++ tests/target/expr.rs | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/src/expr.rs b/src/expr.rs index c5f7ccb857b..2b15ff53787 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -268,10 +268,26 @@ pub fn format_expr( ast::RangeLimits::Closed => "...", }; + 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, + } + } + _ => false, + } + } + match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) { (Some(ref lhs), Some(ref rhs)) => { let sp_delim = if context.config.spaces_around_ranges() { format!(" {} ", delim) + } else if needs_space_before_range(context, lhs) { + format!(" {}", delim) } else { delim.into() }; diff --git a/tests/source/expr.rs b/tests/source/expr.rs index 3d54e68c409..96e00174af8 100644 --- a/tests/source/expr.rs +++ b/tests/source/expr.rs @@ -251,6 +251,10 @@ fn ranges() { let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; let z = ... x ; + // #1766 + let x = [0. ..10.0]; + let x = [0. ...10.0]; + a ... b // the expr below won't compile for some reason... diff --git a/tests/target/expr.rs b/tests/target/expr.rs index 3b275d30f4c..de4391d4056 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -315,6 +315,10 @@ fn ranges() { bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; let z = ...x; + // #1766 + let x = [0. ..10.0]; + let x = [0. ...10.0]; + a...b // the expr below won't compile for some reason...