@@ -105,13 +105,32 @@ fn compare_use_trees(a: &ast::UseTree, b: &ast::UseTree, nested: bool) -> Orderi
105
105
}
106
106
}
107
107
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 > {
109
109
match ( & a. node , & b. node ) {
110
110
( & ast:: ItemKind :: Use ( ref a_tree) , & ast:: ItemKind :: Use ( ref b_tree) ) => {
111
111
Some ( compare_use_trees ( a_tree, b_tree, false ) )
112
112
}
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)
115
134
}
116
135
_ => None ,
117
136
}
@@ -257,7 +276,7 @@ fn rewrite_imports(
257
276
false ,
258
277
) ;
259
278
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 ( ) ) ;
261
280
let item_vec: Vec < _ > = item_pair_vec. into_iter ( ) . map ( |pair| pair. 0 ) . collect ( ) ;
262
281
263
282
let fmt = ListFormatting {
0 commit comments