]> git.lizzy.rs Git - rust.git/commitdiff
Add 'use_field_init_shorthand' config option
authorSeiichi Uchida <seuchida@gmail.com>
Mon, 29 Jan 2018 13:14:25 +0000 (22:14 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Mon, 29 Jan 2018 13:15:20 +0000 (22:15 +0900)
CHANGELOG.md
Configurations.md
src/config.rs
src/expr.rs
tests/source/configs/use_field_init_shorthand/false.rs [new file with mode: 0644]
tests/source/configs/use_field_init_shorthand/true.rs [new file with mode: 0644]
tests/source/init_shorthand.rs [deleted file]
tests/target/closure.rs
tests/target/configs/use_field_init_shorthand/false.rs [new file with mode: 0644]
tests/target/configs/use_field_init_shorthand/true.rs [new file with mode: 0644]
tests/target/init_shorthand.rs [deleted file]

index eb21e634ffa5824745a66a8ee846ec356da54e41..563a28df17c0132ec15877f22c7c897821cc4818 100644 (file)
@@ -2,6 +2,10 @@
 
 ## [Unreleased]
 
+### Added
+
+- Add `use_field_init_shorthand` config option.
+
 ## [0.3.6] 2018-01-18
 
 ### Fixed
index 1b9c13ae39d376ad85311537412f5de2e10cb562..eb693833c468f8f45c1d456163e2d5ca9e6fb412 100644 (file)
@@ -1685,6 +1685,48 @@ fn lorem<Ipsum: Dolor+Sit=Amet>() {
 }
 ```
 
+## `use_field_init_shorthand`
+
+Use field initialize shorthand if possible.
+
+- **Default value**: `false`
+- **Possible values**: `true`, `false`
+- **Stable**: No
+
+#### `false` (default):
+
+```rust
+struct Foo {
+    x: u32,
+    y: u32,
+    z: u32,
+}
+
+fn main() {
+    let x = 1;
+    let y = 2;
+    let z = 3;
+    let a = Foo { x: x, y: y, z: z };
+}
+```
+
+#### `true`:
+
+```rust
+struct Foo {
+    x: u32,
+    y: u32,
+    z: u32,
+}
+
+fn main() {
+    let x = 1;
+    let y = 2;
+    let z = 3;
+    let a = Foo { x, y, z };
+}
+```
+
 ## `use_try_shorthand`
 
 Replace uses of the try! macro by the ? shorthand
index 954858b93a6d757bea3bcef824357d863b810ccb..4483cbe14ebe63a94cdca335d5500888d1162360 100644 (file)
@@ -673,6 +673,7 @@ pub fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
     condense_wildcard_suffixes: bool, false, false, "Replace strings of _ wildcards by a single .. \
                                               in tuple patterns";
     force_explicit_abi: bool, true, true, "Always print the abi for extern items";
+    use_field_init_shorthand: bool, false, false, "Use field initialization shorthand if possible";
 
     // Control options (changes the operation of rustfmt, rather than the formatting)
     write_mode: WriteMode, WriteMode::Overwrite, false,
index d59e383389ff9498f3df8346a54d820f3ff0fc5d..39fa17e0fee40c1782490721ac4c3a83d16b0694 100644 (file)
@@ -2582,7 +2582,9 @@ pub fn rewrite_field(
         let expr = field.expr.rewrite(context, expr_shape);
 
         match expr {
-            Some(ref e) if e.as_str() == name => Some(attrs_str + &name),
+            Some(ref e) if e.as_str() == name && context.config.use_field_init_shorthand() => {
+                Some(attrs_str + &name)
+            }
             Some(e) => Some(format!("{}{}{}{}", attrs_str, name, separator, e)),
             None => {
                 let expr_offset = shape.indent.block_indent(context.config);
diff --git a/tests/source/configs/use_field_init_shorthand/false.rs b/tests/source/configs/use_field_init_shorthand/false.rs
new file mode 100644 (file)
index 0000000..16ce740
--- /dev/null
@@ -0,0 +1,19 @@
+// rustfmt-use_field_init_shorthand: false
+// Use field initialization shorthand if possible.
+
+fn main() {
+    let a = Foo {
+        x: x,
+        y: y,
+        z: z,
+    };
+
+    let b = Bar {
+        x: x,
+        y: y,
+        #[attr]
+        z: z,
+        #[rustfmt_skip]
+        skipped: skipped,
+    };
+}
diff --git a/tests/source/configs/use_field_init_shorthand/true.rs b/tests/source/configs/use_field_init_shorthand/true.rs
new file mode 100644 (file)
index 0000000..1e36c6c
--- /dev/null
@@ -0,0 +1,19 @@
+// rustfmt-use_field_init_shorthand: true
+// Use field initialization shorthand if possible.
+
+fn main() {
+    let a = Foo {
+        x: x,
+        y: y,
+        z: z,
+    };
+
+    let b = Bar {
+        x: x,
+        y: y,
+        #[attr]
+        z: z,
+        #[rustfmt_skip]
+        skipped: skipped,
+    };
+}
diff --git a/tests/source/init_shorthand.rs b/tests/source/init_shorthand.rs
deleted file mode 100644 (file)
index 31c8fa2..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-// Use field initialization shorthand if possible.
-
-fn main() {
-    let a = Foo {
-        x: x,
-        y: y,
-        z: z,
-    };
-
-    let b = Bar {
-        x: x,
-        y: y,
-        #[attr]
-        z: z,
-        #[rustfmt_skip]
-        skipped: skipped,
-    };
-}
index 54ad25b1b377619672b89d59914770b0bf40edc7..5bc89d582229666f9d4a75623776e8b93b029404 100644 (file)
@@ -147,7 +147,7 @@ fn issue470() {
                             hair::PatternRef::Hair(pattern),
                             &lvalue,
                         );
-                        ArgDecl { ty }
+                        ArgDecl { ty: ty }
                     },
                 );
             }
diff --git a/tests/target/configs/use_field_init_shorthand/false.rs b/tests/target/configs/use_field_init_shorthand/false.rs
new file mode 100644 (file)
index 0000000..dcebe0b
--- /dev/null
@@ -0,0 +1,15 @@
+// rustfmt-use_field_init_shorthand: false
+// Use field initialization shorthand if possible.
+
+fn main() {
+    let a = Foo { x: x, y: y, z: z };
+
+    let b = Bar {
+        x: x,
+        y: y,
+        #[attr]
+        z: z,
+        #[rustfmt_skip]
+        skipped: skipped,
+    };
+}
diff --git a/tests/target/configs/use_field_init_shorthand/true.rs b/tests/target/configs/use_field_init_shorthand/true.rs
new file mode 100644 (file)
index 0000000..ad78093
--- /dev/null
@@ -0,0 +1,15 @@
+// rustfmt-use_field_init_shorthand: true
+// Use field initialization shorthand if possible.
+
+fn main() {
+    let a = Foo { x, y, z };
+
+    let b = Bar {
+        x,
+        y,
+        #[attr]
+        z,
+        #[rustfmt_skip]
+        skipped: skipped,
+    };
+}
diff --git a/tests/target/init_shorthand.rs b/tests/target/init_shorthand.rs
deleted file mode 100644 (file)
index be315ff..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-// Use field initialization shorthand if possible.
-
-fn main() {
-    let a = Foo { x, y, z };
-
-    let b = Bar {
-        x,
-        y,
-        #[attr]
-        z,
-        #[rustfmt_skip]
-        skipped: skipped,
-    };
-}