From 911395a45198fa18708da6622b11c4c16a5e52a2 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 12 Mar 2018 17:24:04 +1300 Subject: [PATCH 1/4] Squash the various 'reorder imports' option into one cc #2185 --- Configurations.md | 117 +----------------------- src/config/mod.rs | 9 +- src/imports.rs | 1 + src/lib.rs | 2 +- src/reorder.rs | 12 +-- tests/target/extern.rs | 13 +-- tests/target/import-fencepost-length.rs | 2 +- tests/target/imports.rs | 2 +- 8 files changed, 19 insertions(+), 139 deletions(-) diff --git a/Configurations.md b/Configurations.md index ca804b2f7f2..8ace886a98f 100644 --- a/Configurations.md +++ b/Configurations.md @@ -6,7 +6,7 @@ A possible content of `rustfmt.toml` or `.rustfmt.toml` might look like this: ```toml indent_style = "Block" -reorder_imported_names = true +reorder_imports = false ``` Each configuration option is either stable or unstable. @@ -1240,31 +1240,10 @@ fn dolor() -> usize {} fn adipiscing() -> usize {} ``` -## `reorder_imported_names` - -Reorder lists of names in import statements alphabetically - -- **Default value**: `false` -- **Possible values**: `true`, `false` -- **Stable**: No - -#### `false` (default): - -```rust -use super::{lorem, ipsum, dolor, sit}; -``` - -#### `true`: - -```rust -use super::{dolor, ipsum, lorem, sit}; -``` - -See also [`reorder_imports`](#reorder_imports). ## `reorder_imports` -Reorder import statements alphabetically +Reorder import and extern crate statements alphabetically - **Default value**: `false` - **Possible values**: `true`, `false` @@ -1288,98 +1267,6 @@ use lorem; use sit; ``` -See also [`reorder_imported_names`](#reorder_imported_names), [`reorder_imports_in_group`](#reorder_imports_in_group). - -## `reorder_imports_in_group` - -Reorder import statements in group - -- **Default value**: `false` -- **Possible values**: `true`, `false` -- **Stable**: No - -**Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`. - -#### `true` (default): - -```rust -use std::io; -use std::mem; - -use dolor; -use ipsum; -use lorem; -use sit; -``` - -#### `false`: - - -```rust -use dolor; -use ipsum; -use lorem; -use sit; -use std::io; -use std::mem; -``` - -See also [`reorder_imports`](#reorder_imports). - -## `reorder_extern_crates` - -Reorder `extern crate` statements alphabetically - -- **Default value**: `true` -- **Possible values**: `true`, `false` -- **Stable**: No - -#### `true` (default): - -```rust -extern crate dolor; -extern crate ipsum; -extern crate lorem; -extern crate sit; -``` - -#### `false`: - -```rust -extern crate lorem; -extern crate ipsum; - -extern crate dolor; -extern crate sit; -``` - -See also [`reorder_extern_crates_in_group`](#reorder_extern_crates_in_group). - -## `reorder_extern_crates_in_group` - -Reorder `extern crate` statements in group - -- **Default value**: `true` -- **Possible values**: `true`, `false` -- **Stable**: No - -#### `false` (default): - -This value has no influence beyond the effect of the [`reorder_extern_crates`](#reorder_extern_crates) option. Set [`reorder_extern_crates`](#reorder_extern_crates) to `false` if you do not want `extern crate` groups to be collapsed and ordered. - -#### `true`: - -**Note:** This only takes effect when [`reorder_extern_crates`](#reorder_extern_crates) is set to `true`. - -```rust -extern crate a; -extern crate b; - -extern crate dolor; -extern crate ipsum; -extern crate lorem; -extern crate sit; -``` ## `reorder_modules` diff --git a/src/config/mod.rs b/src/config/mod.rs index 3676aed4ba9..63a75f880b6 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -68,14 +68,9 @@ create_config! { imports_layout: ListTactic, ListTactic::Mixed, false, "Item layout inside a import block"; // Ordering - reorder_extern_crates: bool, true, false, "Reorder extern crate statements alphabetically"; - reorder_extern_crates_in_group: bool, true, false, "Reorder extern crate statements in group"; - reorder_imports: bool, true, false, "Reorder import statements alphabetically"; - reorder_imports_in_group: bool, true, false, "Reorder import statements in group"; - reorder_imported_names: bool, true, false, - "Reorder lists of names in import statements alphabetically"; - reorder_modules: bool, true, false, "Reorder module statemtents alphabetically in group"; reorder_impl_items: bool, false, false, "Reorder impl items"; + reorder_imports: bool, true, false, "Reorder import and extern crate statements alphabetically"; + reorder_modules: bool, true, false, "Reorder module statements alphabetically in group"; // Spaces around punctuation type_punctuation_density: TypeDensity, TypeDensity::Wide, false, diff --git a/src/imports.rs b/src/imports.rs index 9a02ee64fb7..62273a86596 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -485,6 +485,7 @@ fn rewrite_nested_use_tree( ); (tactic, remaining_width) }; + let ends_with_newline = context.config.imports_indent() == IndentStyle::Block && tactic != DefinitiveListTactic::Horizontal; let fmt = ListFormatting { diff --git a/src/lib.rs b/src/lib.rs index 9a7d0d2f881..61eeb9332a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,8 +42,8 @@ use std::time::Duration; use syntax::ast; pub use syntax::codemap::FileName; use syntax::codemap::{CodeMap, FilePathMapping}; -use syntax::errors::emitter::{ColorConfig, EmitterWriter}; use syntax::errors::{DiagnosticBuilder, Handler}; +use syntax::errors::emitter::{ColorConfig, EmitterWriter}; use syntax::parse::{self, ParseSess}; use checkstyle::{output_footer, output_header}; diff --git a/src/reorder.rs b/src/reorder.rs index 15191995d02..273f20c9254 100644 --- a/src/reorder.rs +++ b/src/reorder.rs @@ -198,18 +198,18 @@ impl ReorderableItemKind { pub fn is_reorderable(&self, config: &Config) -> bool { match *self { - ReorderableItemKind::ExternCrate => config.reorder_extern_crates(), + ReorderableItemKind::ExternCrate => config.reorder_imports(), ReorderableItemKind::Mod => config.reorder_modules(), ReorderableItemKind::Use => config.reorder_imports(), ReorderableItemKind::Other => false, } } - pub fn in_group(&self, config: &Config) -> bool { + pub fn in_group(&self) -> bool { match *self { - ReorderableItemKind::ExternCrate => config.reorder_extern_crates_in_group(), - ReorderableItemKind::Mod => config.reorder_modules(), - ReorderableItemKind::Use => config.reorder_imports_in_group(), + ReorderableItemKind::ExternCrate => false, + ReorderableItemKind::Mod => true, + ReorderableItemKind::Use => true, ReorderableItemKind::Other => false, } } @@ -268,7 +268,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { let item_kind = ReorderableItemKind::from(items[0]); if item_kind.is_reorderable(self.config) { let visited_items_num = - self.walk_reorderable_items(items, item_kind, item_kind.in_group(self.config)); + self.walk_reorderable_items(items, item_kind, item_kind.in_group()); let (_, rest) = items.split_at(visited_items_num); items = rest; } else { diff --git a/tests/target/extern.rs b/tests/target/extern.rs index b0aa51127d5..61989cace75 100644 --- a/tests/target/extern.rs +++ b/tests/target/extern.rs @@ -1,17 +1,14 @@ // rustfmt-normalize_comments: true -extern crate foo; -extern crate foo as bar; - +extern crate bar; extern crate chrono; extern crate dotenv; -extern crate futures; - -extern crate bar; extern crate foo; - -// #2315 +extern crate foo; +extern crate foo as bar; +extern crate futures; extern crate proc_macro; +// #2315 extern crate proc_macro2; extern "C" { diff --git a/tests/target/import-fencepost-length.rs b/tests/target/import-fencepost-length.rs index e4f885c09b1..9d247aaf6df 100644 --- a/tests/target/import-fencepost-length.rs +++ b/tests/target/import-fencepost-length.rs @@ -1,4 +1,4 @@ -use aaaaaaaaaaaaaaa::bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; use aaaaaaaaaaaaaaa::{bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccccccc, dddddddd}; use aaaaaaaaaaaaaaa::{bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccccccc, ddddddddd}; +use aaaaaaaaaaaaaaa::bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; diff --git a/tests/target/imports.rs b/tests/target/imports.rs index 236fee95e82..b6d939fc125 100644 --- a/tests/target/imports.rs +++ b/tests/target/imports.rs @@ -54,9 +54,9 @@ use foo::{baz, qux as bar}; // With absolute paths use foo; +use Foo; use foo::Bar; use foo::{Bar, Baz}; -use Foo; use {Bar, Baz}; // Root globs From aa4419019a90de71646481777ca86b15bef9d00e Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Fri, 16 Mar 2018 21:28:02 +1300 Subject: [PATCH 2/4] Update tests --- tests/source/configs/reorder_extern_crates/false.rs | 11 ----------- tests/source/configs/reorder_extern_crates/true.rs | 11 ----------- .../source/configs/reorder_imported_names/false.rs | 4 ---- tests/source/configs/reorder_imported_names/true.rs | 4 ---- .../configs/reorder_imports_in_group/false.rs | 13 ------------- .../source/configs/reorder_imports_in_group/true.rs | 13 ------------- tests/source/imports-reorder-lines-and-items.rs | 3 --- tests/source/imports-reorder.rs | 1 - tests/target/configs/reorder_extern_crates/false.rs | 11 ----------- tests/target/configs/reorder_extern_crates/true.rs | 11 ----------- .../target/configs/reorder_imported_names/false.rs | 4 ---- tests/target/configs/reorder_imported_names/true.rs | 4 ---- .../configs/reorder_imports_in_group/false.rs | 12 ------------ .../target/configs/reorder_imports_in_group/true.rs | 13 ------------- tests/target/imports-reorder-lines-and-items.rs | 3 --- tests/target/imports-reorder.rs | 1 - 16 files changed, 119 deletions(-) delete mode 100644 tests/source/configs/reorder_extern_crates/false.rs delete mode 100644 tests/source/configs/reorder_extern_crates/true.rs delete mode 100644 tests/source/configs/reorder_imported_names/false.rs delete mode 100644 tests/source/configs/reorder_imported_names/true.rs delete mode 100644 tests/source/configs/reorder_imports_in_group/false.rs delete mode 100644 tests/source/configs/reorder_imports_in_group/true.rs delete mode 100644 tests/target/configs/reorder_extern_crates/false.rs delete mode 100644 tests/target/configs/reorder_extern_crates/true.rs delete mode 100644 tests/target/configs/reorder_imported_names/false.rs delete mode 100644 tests/target/configs/reorder_imported_names/true.rs delete mode 100644 tests/target/configs/reorder_imports_in_group/false.rs delete mode 100644 tests/target/configs/reorder_imports_in_group/true.rs diff --git a/tests/source/configs/reorder_extern_crates/false.rs b/tests/source/configs/reorder_extern_crates/false.rs deleted file mode 100644 index 6bef132e5c3..00000000000 --- a/tests/source/configs/reorder_extern_crates/false.rs +++ /dev/null @@ -1,11 +0,0 @@ -// rustfmt-reorder_extern_crates: false - -extern crate foo; -extern crate bar; -extern crate foobar; - -#[macro_use] -extern crate nom; -extern crate regex; -#[macro_use] -extern crate log; diff --git a/tests/source/configs/reorder_extern_crates/true.rs b/tests/source/configs/reorder_extern_crates/true.rs deleted file mode 100644 index bdf00f57cda..00000000000 --- a/tests/source/configs/reorder_extern_crates/true.rs +++ /dev/null @@ -1,11 +0,0 @@ -// rustfmt-reorder_extern_crates: true - -extern crate foo; -extern crate bar; -extern crate foobar; - -#[macro_use] -extern crate nom; -extern crate regex; -#[macro_use] -extern crate log; diff --git a/tests/source/configs/reorder_imported_names/false.rs b/tests/source/configs/reorder_imported_names/false.rs deleted file mode 100644 index 91db89ceeaa..00000000000 --- a/tests/source/configs/reorder_imported_names/false.rs +++ /dev/null @@ -1,4 +0,0 @@ -// rustfmt-reorder_imported_names: false -// Reorder imported names - -use super::{lorem, ipsum, dolor, sit}; diff --git a/tests/source/configs/reorder_imported_names/true.rs b/tests/source/configs/reorder_imported_names/true.rs deleted file mode 100644 index 69da6186d5a..00000000000 --- a/tests/source/configs/reorder_imported_names/true.rs +++ /dev/null @@ -1,4 +0,0 @@ -// rustfmt-reorder_imported_names: true -// Reorder imported names - -use super::{lorem, ipsum, dolor, sit}; diff --git a/tests/source/configs/reorder_imports_in_group/false.rs b/tests/source/configs/reorder_imports_in_group/false.rs deleted file mode 100644 index 87711bb142b..00000000000 --- a/tests/source/configs/reorder_imports_in_group/false.rs +++ /dev/null @@ -1,13 +0,0 @@ -// rustfmt-reorder_imports: true -// rustfmt-reorder_imports_in_group: false -// Reorder imports in group - -/// This comment should stay with `use std::mem;` -use std::mem; -use std::io; - -use lorem; -/// This comment should stay with `use ipsum;` -use ipsum; -use dolor; -use sit; diff --git a/tests/source/configs/reorder_imports_in_group/true.rs b/tests/source/configs/reorder_imports_in_group/true.rs deleted file mode 100644 index b5690b89cc0..00000000000 --- a/tests/source/configs/reorder_imports_in_group/true.rs +++ /dev/null @@ -1,13 +0,0 @@ -// rustfmt-reorder_imports: true -// rustfmt-reorder_imports_in_group: true -// Reorder imports in group - -/// This comment should stay with `use std::mem;` -use std::mem; -use std::io; - -use lorem; -/// This comment should stay with `use ipsum;` -use ipsum; -use dolor; -use sit; diff --git a/tests/source/imports-reorder-lines-and-items.rs b/tests/source/imports-reorder-lines-and-items.rs index 3c71f9984fe..b6380f31c61 100644 --- a/tests/source/imports-reorder-lines-and-items.rs +++ b/tests/source/imports-reorder-lines-and-items.rs @@ -1,6 +1,3 @@ -// rustfmt-reorder_imports: true -// rustfmt-reorder_imported_names: true - /// This comment should stay with `use std::str;` use std::str; use std::cmp::{d, c, b, a}; diff --git a/tests/source/imports-reorder.rs b/tests/source/imports-reorder.rs index 200cad2d13c..cbe9d6ca78a 100644 --- a/tests/source/imports-reorder.rs +++ b/tests/source/imports-reorder.rs @@ -1,5 +1,4 @@ // rustfmt-normalize_comments: true -// rustfmt-reorder_imported_names: true use path::{C,/*A*/ A, B /* B */, self /* self */}; diff --git a/tests/target/configs/reorder_extern_crates/false.rs b/tests/target/configs/reorder_extern_crates/false.rs deleted file mode 100644 index 6bef132e5c3..00000000000 --- a/tests/target/configs/reorder_extern_crates/false.rs +++ /dev/null @@ -1,11 +0,0 @@ -// rustfmt-reorder_extern_crates: false - -extern crate foo; -extern crate bar; -extern crate foobar; - -#[macro_use] -extern crate nom; -extern crate regex; -#[macro_use] -extern crate log; diff --git a/tests/target/configs/reorder_extern_crates/true.rs b/tests/target/configs/reorder_extern_crates/true.rs deleted file mode 100644 index 86aba38b5d1..00000000000 --- a/tests/target/configs/reorder_extern_crates/true.rs +++ /dev/null @@ -1,11 +0,0 @@ -// rustfmt-reorder_extern_crates: true - -extern crate bar; -extern crate foo; -extern crate foobar; - -#[macro_use] -extern crate nom; -extern crate regex; -#[macro_use] -extern crate log; diff --git a/tests/target/configs/reorder_imported_names/false.rs b/tests/target/configs/reorder_imported_names/false.rs deleted file mode 100644 index 91db89ceeaa..00000000000 --- a/tests/target/configs/reorder_imported_names/false.rs +++ /dev/null @@ -1,4 +0,0 @@ -// rustfmt-reorder_imported_names: false -// Reorder imported names - -use super::{lorem, ipsum, dolor, sit}; diff --git a/tests/target/configs/reorder_imported_names/true.rs b/tests/target/configs/reorder_imported_names/true.rs deleted file mode 100644 index 59f55f4718a..00000000000 --- a/tests/target/configs/reorder_imported_names/true.rs +++ /dev/null @@ -1,4 +0,0 @@ -// rustfmt-reorder_imported_names: true -// Reorder imported names - -use super::{dolor, ipsum, lorem, sit}; diff --git a/tests/target/configs/reorder_imports_in_group/false.rs b/tests/target/configs/reorder_imports_in_group/false.rs deleted file mode 100644 index 29460da50aa..00000000000 --- a/tests/target/configs/reorder_imports_in_group/false.rs +++ /dev/null @@ -1,12 +0,0 @@ -// rustfmt-reorder_imports: true -// rustfmt-reorder_imports_in_group: false -// Reorder imports in group - -use dolor; -/// This comment should stay with `use ipsum;` -use ipsum; -use lorem; -use sit; -use std::io; -/// This comment should stay with `use std::mem;` -use std::mem; diff --git a/tests/target/configs/reorder_imports_in_group/true.rs b/tests/target/configs/reorder_imports_in_group/true.rs deleted file mode 100644 index c5e353662b5..00000000000 --- a/tests/target/configs/reorder_imports_in_group/true.rs +++ /dev/null @@ -1,13 +0,0 @@ -// rustfmt-reorder_imports: true -// rustfmt-reorder_imports_in_group: true -// Reorder imports in group - -use std::io; -/// This comment should stay with `use std::mem;` -use std::mem; - -use dolor; -/// This comment should stay with `use ipsum;` -use ipsum; -use lorem; -use sit; diff --git a/tests/target/imports-reorder-lines-and-items.rs b/tests/target/imports-reorder-lines-and-items.rs index e31819be2c0..98a5afe4348 100644 --- a/tests/target/imports-reorder-lines-and-items.rs +++ b/tests/target/imports-reorder-lines-and-items.rs @@ -1,6 +1,3 @@ -// rustfmt-reorder_imports: true -// rustfmt-reorder_imported_names: true - use std::cmp::{a, b, c, d}; use std::ddd::aaa; use std::ddd::{a, b, c as g, d as p}; diff --git a/tests/target/imports-reorder.rs b/tests/target/imports-reorder.rs index fbb56e3aa0c..84e97c0224f 100644 --- a/tests/target/imports-reorder.rs +++ b/tests/target/imports-reorder.rs @@ -1,5 +1,4 @@ // rustfmt-normalize_comments: true -// rustfmt-reorder_imported_names: true use path::{self /* self */, /* A */ A, B /* B */, C}; From 1c17dbbf74ada52bed7545251e7da94e82b6e520 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Fri, 6 Apr 2018 21:11:50 +1200 Subject: [PATCH 3/4] rebasing --- src/imports.rs | 79 +++++++++++-------------- src/lib.rs | 2 +- src/reorder.rs | 2 +- tests/target/extern.rs | 13 ++-- tests/target/import-fencepost-length.rs | 2 +- tests/target/imports.rs | 2 +- 6 files changed, 45 insertions(+), 55 deletions(-) diff --git a/src/imports.rs b/src/imports.rs index 62273a86596..48b6af7fd46 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -185,7 +185,7 @@ impl UseTree { } else { Some(item.attrs.clone()) }, - ).normalize(context.config.reorder_imported_names()), + ).normalize(), ), _ => None, } @@ -271,7 +271,7 @@ impl UseTree { } // Do the adjustments that rustfmt does elsewhere to use paths. - pub fn normalize(mut self, do_sort: bool) -> UseTree { + pub fn normalize(mut self) -> UseTree { let mut last = self.path.pop().expect("Empty use tree?"); // Hack around borrow checker. let mut normalize_sole_list = false; @@ -340,7 +340,7 @@ impl UseTree { for seg in &list[0].path { self.path.push(seg.clone()); } - return self.normalize(do_sort); + return self.normalize(); } _ => unreachable!(), } @@ -349,11 +349,9 @@ impl UseTree { // Recursively normalize elements of a list use (including sorting the list). if let UseSegment::List(list) = last { let mut list = list.into_iter() - .map(|ut| ut.normalize(do_sort)) + .map(|ut| ut.normalize()) .collect::>(); - if do_sort { - list.sort(); - } + list.sort(); last = UseSegment::List(list); } @@ -686,75 +684,64 @@ mod test { #[test] fn test_use_tree_normalize() { + assert_eq!(parse_use_tree("a::self").normalize(), parse_use_tree("a")); assert_eq!( - parse_use_tree("a::self").normalize(true), - parse_use_tree("a") - ); - assert_eq!( - parse_use_tree("a::self as foo").normalize(true), + parse_use_tree("a::self as foo").normalize(), parse_use_tree("a as foo") ); + assert_eq!(parse_use_tree("a::{self}").normalize(), parse_use_tree("a")); + assert_eq!(parse_use_tree("a::{b}").normalize(), parse_use_tree("a::b")); assert_eq!( - parse_use_tree("a::{self}").normalize(true), - parse_use_tree("a") - ); - assert_eq!( - parse_use_tree("a::{b}").normalize(true), - parse_use_tree("a::b") - ); - assert_eq!( - parse_use_tree("a::{b, c::self}").normalize(true), + parse_use_tree("a::{b, c::self}").normalize(), parse_use_tree("a::{b, c}") ); assert_eq!( - parse_use_tree("a::{b as bar, c::self}").normalize(true), + parse_use_tree("a::{b as bar, c::self}").normalize(), parse_use_tree("a::{b as bar, c}") ); } #[test] fn test_use_tree_ord() { - assert!(parse_use_tree("a").normalize(true) < parse_use_tree("aa").normalize(true)); - assert!(parse_use_tree("a").normalize(true) < parse_use_tree("a::a").normalize(true)); - assert!(parse_use_tree("a").normalize(true) < parse_use_tree("*").normalize(true)); - assert!(parse_use_tree("a").normalize(true) < parse_use_tree("{a, b}").normalize(true)); - assert!(parse_use_tree("*").normalize(true) < parse_use_tree("{a, b}").normalize(true)); + assert!(parse_use_tree("a").normalize() < parse_use_tree("aa").normalize()); + assert!(parse_use_tree("a").normalize() < parse_use_tree("a::a").normalize()); + assert!(parse_use_tree("a").normalize() < parse_use_tree("*").normalize()); + assert!(parse_use_tree("a").normalize() < parse_use_tree("{a, b}").normalize()); + assert!(parse_use_tree("*").normalize() < parse_use_tree("{a, b}").normalize()); assert!( - parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, dddddddd}").normalize(true) - < parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, ddddddddd}").normalize(true) + parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, dddddddd}").normalize() + < parse_use_tree("aaaaaaaaaaaaaaa::{bb, cc, ddddddddd}").normalize() ); assert!( - parse_use_tree("serde::de::{Deserialize}").normalize(true) - < parse_use_tree("serde_json").normalize(true) + parse_use_tree("serde::de::{Deserialize}").normalize() + < parse_use_tree("serde_json").normalize() ); + assert!(parse_use_tree("a::b::c").normalize() < parse_use_tree("a::b::*").normalize()); assert!( - parse_use_tree("a::b::c").normalize(true) < parse_use_tree("a::b::*").normalize(true) - ); - assert!( - parse_use_tree("foo::{Bar, Baz}").normalize(true) - < parse_use_tree("{Bar, Baz}").normalize(true) + parse_use_tree("foo::{Bar, Baz}").normalize() + < parse_use_tree("{Bar, Baz}").normalize() ); assert!( - parse_use_tree("foo::{self as bar}").normalize(true) - < parse_use_tree("foo::{qux as bar}").normalize(true) + parse_use_tree("foo::{self as bar}").normalize() + < parse_use_tree("foo::{qux as bar}").normalize() ); assert!( - parse_use_tree("foo::{qux as bar}").normalize(true) - < parse_use_tree("foo::{baz, qux as bar}").normalize(true) + parse_use_tree("foo::{qux as bar}").normalize() + < parse_use_tree("foo::{baz, qux as bar}").normalize() ); assert!( - parse_use_tree("foo::{self as bar, baz}").normalize(true) - < parse_use_tree("foo::{baz, qux as bar}").normalize(true) + parse_use_tree("foo::{self as bar, baz}").normalize() + < parse_use_tree("foo::{baz, qux as bar}").normalize() ); - assert!(parse_use_tree("foo").normalize(true) < parse_use_tree("Foo").normalize(true)); - assert!(parse_use_tree("foo").normalize(true) < parse_use_tree("foo::Bar").normalize(true)); + assert!(parse_use_tree("foo").normalize() < parse_use_tree("Foo").normalize()); + assert!(parse_use_tree("foo").normalize() < parse_use_tree("foo::Bar").normalize()); assert!( - parse_use_tree("std::cmp::{d, c, b, a}").normalize(true) - < parse_use_tree("std::cmp::{b, e, g, f}").normalize(true) + parse_use_tree("std::cmp::{d, c, b, a}").normalize() + < parse_use_tree("std::cmp::{b, e, g, f}").normalize() ); } } diff --git a/src/lib.rs b/src/lib.rs index 61eeb9332a6..9a7d0d2f881 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,8 +42,8 @@ use std::time::Duration; use syntax::ast; pub use syntax::codemap::FileName; use syntax::codemap::{CodeMap, FilePathMapping}; -use syntax::errors::{DiagnosticBuilder, Handler}; use syntax::errors::emitter::{ColorConfig, EmitterWriter}; +use syntax::errors::{DiagnosticBuilder, Handler}; use syntax::parse::{self, ParseSess}; use checkstyle::{output_footer, output_header}; diff --git a/src/reorder.rs b/src/reorder.rs index 273f20c9254..e4065146cc6 100644 --- a/src/reorder.rs +++ b/src/reorder.rs @@ -207,7 +207,7 @@ impl ReorderableItemKind { pub fn in_group(&self) -> bool { match *self { - ReorderableItemKind::ExternCrate => false, + ReorderableItemKind::ExternCrate => true, ReorderableItemKind::Mod => true, ReorderableItemKind::Use => true, ReorderableItemKind::Other => false, diff --git a/tests/target/extern.rs b/tests/target/extern.rs index 61989cace75..b0aa51127d5 100644 --- a/tests/target/extern.rs +++ b/tests/target/extern.rs @@ -1,14 +1,17 @@ // rustfmt-normalize_comments: true -extern crate bar; -extern crate chrono; -extern crate dotenv; -extern crate foo; extern crate foo; extern crate foo as bar; + +extern crate chrono; +extern crate dotenv; extern crate futures; -extern crate proc_macro; + +extern crate bar; +extern crate foo; + // #2315 +extern crate proc_macro; extern crate proc_macro2; extern "C" { diff --git a/tests/target/import-fencepost-length.rs b/tests/target/import-fencepost-length.rs index 9d247aaf6df..e4f885c09b1 100644 --- a/tests/target/import-fencepost-length.rs +++ b/tests/target/import-fencepost-length.rs @@ -1,4 +1,4 @@ +use aaaaaaaaaaaaaaa::bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; use aaaaaaaaaaaaaaa::{bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccccccc, dddddddd}; use aaaaaaaaaaaaaaa::{bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccccccc, ddddddddd}; -use aaaaaaaaaaaaaaa::bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; diff --git a/tests/target/imports.rs b/tests/target/imports.rs index b6d939fc125..236fee95e82 100644 --- a/tests/target/imports.rs +++ b/tests/target/imports.rs @@ -54,9 +54,9 @@ use foo::{baz, qux as bar}; // With absolute paths use foo; -use Foo; use foo::Bar; use foo::{Bar, Baz}; +use Foo; use {Bar, Baz}; // Root globs From 305c6405534f9b33f44ccb5b9de87bd00f006ef3 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Wed, 11 Apr 2018 10:50:19 +1200 Subject: [PATCH 4/4] Review changes --- Configurations.md | 5 +++-- src/config/mod.rs | 2 +- src/reorder.rs | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Configurations.md b/Configurations.md index 8ace886a98f..26d540b70b5 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1243,9 +1243,10 @@ fn adipiscing() -> usize {} ## `reorder_imports` -Reorder import and extern crate statements alphabetically +Reorder import and extern crate statements alphabetically in groups (a group is +separated by a newline). -- **Default value**: `false` +- **Default value**: `true` - **Possible values**: `true`, `false` - **Stable**: No diff --git a/src/config/mod.rs b/src/config/mod.rs index 63a75f880b6..62d025ad5a1 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -68,9 +68,9 @@ create_config! { imports_layout: ListTactic, ListTactic::Mixed, false, "Item layout inside a import block"; // Ordering - reorder_impl_items: bool, false, false, "Reorder impl items"; reorder_imports: bool, true, false, "Reorder import and extern crate statements alphabetically"; reorder_modules: bool, true, false, "Reorder module statements alphabetically in group"; + reorder_impl_items: bool, false, false, "Reorder impl items"; // Spaces around punctuation type_punctuation_density: TypeDensity, TypeDensity::Wide, false, diff --git a/src/reorder.rs b/src/reorder.rs index e4065146cc6..ed307e679dd 100644 --- a/src/reorder.rs +++ b/src/reorder.rs @@ -207,9 +207,9 @@ impl ReorderableItemKind { pub fn in_group(&self) -> bool { match *self { - ReorderableItemKind::ExternCrate => true, - ReorderableItemKind::Mod => true, - ReorderableItemKind::Use => true, + ReorderableItemKind::ExternCrate + | ReorderableItemKind::Mod + | ReorderableItemKind::Use => true, ReorderableItemKind::Other => false, } }