]> git.lizzy.rs Git - rust.git/commitdiff
Add spaces between consecutive `..` `..=`
authorShotaro Yamada <sinkuu@sinkuu.xyz>
Mon, 16 Apr 2018 08:11:50 +0000 (17:11 +0900)
committerShotaro Yamada <sinkuu@sinkuu.xyz>
Mon, 16 Apr 2018 08:33:22 +0000 (17:33 +0900)
src/expr.rs
tests/source/expr.rs
tests/target/expr.rs

index 5f786f965004c976efc63b7ccb4a864b60f1e389..84ecbb5718444ceb91d401b1147d07b306466bde 100644 (file)
@@ -248,14 +248,37 @@ fn needs_space_before_range(context: &RewriteContext, lhs: &ast::Expr) -> bool {
                 }
             }
 
+            fn needs_space_after_range(rhs: &ast::Expr) -> bool {
+                match rhs.node {
+                    // Don't format `.. ..` into `....`, which is invalid.
+                    //
+                    // This check is unnecessary for `lhs`, because a range
+                    // starting from another range needs parentheses as `(x ..) ..`
+                    // (`x .. ..` is a range from `x` to `..`).
+                    ast::ExprKind::Range(None, _, _) => true,
+                    _ => false,
+                }
+            }
+
+            let default_sp_delim = |lhs: Option<&ast::Expr>, rhs: Option<&ast::Expr>| {
+                let space_if = |b: bool| if b { " " } else { "" };
+
+                format!(
+                    "{}{}{}",
+                    lhs.map(|lhs| space_if(needs_space_before_range(context, lhs)))
+                        .unwrap_or(""),
+                    delim,
+                    rhs.map(|rhs| space_if(needs_space_after_range(rhs)))
+                        .unwrap_or(""),
+                )
+            };
+
             match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) {
                 (Some(lhs), Some(rhs)) => {
                     let sp_delim = if context.config.spaces_around_ranges() {
                         format!(" {} ", delim)
-                    } else if needs_space_before_range(context, lhs) {
-                        format!(" {}", delim)
                     } else {
-                        delim.to_owned()
+                        default_sp_delim(Some(lhs), Some(rhs))
                     };
                     rewrite_pair(
                         &*lhs,
@@ -270,7 +293,7 @@ fn needs_space_before_range(context: &RewriteContext, lhs: &ast::Expr) -> bool {
                     let sp_delim = if context.config.spaces_around_ranges() {
                         format!("{} ", delim)
                     } else {
-                        delim.to_owned()
+                        default_sp_delim(None, Some(rhs))
                     };
                     rewrite_unary_prefix(context, &sp_delim, &*rhs, shape)
                 }
@@ -278,7 +301,7 @@ fn needs_space_before_range(context: &RewriteContext, lhs: &ast::Expr) -> bool {
                     let sp_delim = if context.config.spaces_around_ranges() {
                         format!(" {}", delim)
                     } else {
-                        delim.to_owned()
+                        default_sp_delim(Some(lhs), None)
                     };
                     rewrite_unary_suffix(context, &sp_delim, &*lhs, shape)
                 }
index 6367dbc95b8e28ae23ad92e082f4f36d324c7ba1..d5b0babc5bbcaaef25f1797c6c8626e2943d6bb0 100644 (file)
@@ -384,3 +384,9 @@ fn bar(&self) {
     }
 }
 }
+
+fn dots() {
+    .. .. ..; // (.. (.. (..)))
+    ..= ..= ..;
+    (..) .. ..; // ((..) .. (..))
+}
index 09e1e8afd1ada7db341a096a576e0e14fb13c1ed..911a44f063082ff7b16d95a756d37dff80de9831 100644 (file)
@@ -409,3 +409,9 @@ fn bar(&self) {
         }
     }
 }
+
+fn dots() {
+    .. .. ..; // (.. (.. (..)))
+    ..= ..= ..;
+    (..).. ..; // ((..) .. (..))
+}