Skip to content

Commit 75cb236

Browse files
committed
Compare extern crate items with its name instead of span
When we use span, bugs easily sneak in.
1 parent 0f24bc0 commit 75cb236

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

src/imports.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,32 @@ fn compare_use_trees(a: &ast::UseTree, b: &ast::UseTree, nested: bool) -> Orderi
105105
}
106106
}
107107

108-
fn compare_use_items(context: &RewriteContext, a: &ast::Item, b: &ast::Item) -> Option<Ordering> {
108+
fn compare_use_items(a: &ast::Item, b: &ast::Item) -> Option<Ordering> {
109109
match (&a.node, &b.node) {
110110
(&ast::ItemKind::Use(ref a_tree), &ast::ItemKind::Use(ref b_tree)) => {
111111
Some(compare_use_trees(a_tree, b_tree, false))
112112
}
113-
(&ast::ItemKind::ExternCrate(..), &ast::ItemKind::ExternCrate(..)) => {
114-
Some(context.snippet(a.span).cmp(context.snippet(b.span)))
113+
(&ast::ItemKind::ExternCrate(ref a_name), &ast::ItemKind::ExternCrate(ref b_name)) => {
114+
// `extern crate foo as bar;`
115+
// ^^^ Comparing this.
116+
let a_orig_name =
117+
a_name.map_or_else(|| a.ident.name.as_str(), |symbol| symbol.as_str());
118+
let b_orig_name =
119+
b_name.map_or_else(|| b.ident.name.as_str(), |symbol| symbol.as_str());
120+
let result = a_orig_name.cmp(&b_orig_name);
121+
if result != Ordering::Equal {
122+
return Some(result);
123+
}
124+
125+
// `extern crate foo as bar;`
126+
// ^^^ Comparing this.
127+
let result = match (a_name, b_name) {
128+
(Some(..), None) => Ordering::Greater,
129+
(None, Some(..)) => Ordering::Less,
130+
(None, None) => Ordering::Equal,
131+
(Some(..), Some(..)) => a.ident.name.cmp(&b.ident.name),
132+
};
133+
Some(result)
115134
}
116135
_ => None,
117136
}
@@ -257,7 +276,7 @@ fn rewrite_imports(
257276
false,
258277
);
259278
let mut item_pair_vec: Vec<_> = items.zip(use_items.iter()).collect();
260-
item_pair_vec.sort_by(|a, b| compare_use_items(context, a.1, b.1).unwrap());
279+
item_pair_vec.sort_by(|a, b| compare_use_items(a.1, b.1).unwrap());
261280
let item_vec: Vec<_> = item_pair_vec.into_iter().map(|pair| pair.0).collect();
262281

263282
let fmt = ListFormatting {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(match_default_bindings)]
1112
#![feature(rustc_private)]
1213
#![feature(type_ascription)]
1314

tests/source/extern.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ extern crate chrono;
1010
extern crate foo;
1111
extern crate bar;
1212

13+
// #2315
14+
extern crate proc_macro2;
15+
extern crate proc_macro;
16+
1317
extern "C" {
1418
fn c_func(x: *mut *mut libc::c_void);
1519

tests/target/extern.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// rustfmt-normalize_comments: true
22

3-
extern crate foo as bar;
43
extern crate foo;
4+
extern crate foo as bar;
55

66
extern crate chrono;
77
extern crate dotenv;
@@ -10,6 +10,10 @@ extern crate futures;
1010
extern crate bar;
1111
extern crate foo;
1212

13+
// #2315
14+
extern crate proc_macro;
15+
extern crate proc_macro2;
16+
1317
extern "C" {
1418
fn c_func(x: *mut *mut libc::c_void);
1519

0 commit comments

Comments
 (0)