Merge pull request #2316 from topecongiro/issue-2315

Remove trailing comma from extern items snippet before comparing
This commit is contained in:
Nick Cameron 2018-01-04 17:49:47 +13:00 committed by GitHub
commit 91a332483b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 22 deletions

View File

@ -105,13 +105,32 @@ fn compare_use_trees(a: &ast::UseTree, b: &ast::UseTree, nested: bool) -> Orderi
}
}
fn compare_use_items(context: &RewriteContext, a: &ast::Item, b: &ast::Item) -> Option<Ordering> {
fn compare_use_items(a: &ast::Item, b: &ast::Item) -> Option<Ordering> {
match (&a.node, &b.node) {
(&ast::ItemKind::Use(ref a_tree), &ast::ItemKind::Use(ref b_tree)) => {
Some(compare_use_trees(a_tree, b_tree, false))
}
(&ast::ItemKind::ExternCrate(..), &ast::ItemKind::ExternCrate(..)) => {
Some(context.snippet(a.span).cmp(context.snippet(b.span)))
(&ast::ItemKind::ExternCrate(ref a_name), &ast::ItemKind::ExternCrate(ref b_name)) => {
// `extern crate foo as bar;`
// ^^^ Comparing this.
let a_orig_name =
a_name.map_or_else(|| a.ident.name.as_str(), |symbol| symbol.as_str());
let b_orig_name =
b_name.map_or_else(|| b.ident.name.as_str(), |symbol| symbol.as_str());
let result = a_orig_name.cmp(&b_orig_name);
if result != Ordering::Equal {
return Some(result);
}
// `extern crate foo as bar;`
// ^^^ Comparing this.
let result = match (a_name, b_name) {
(Some(..), None) => Ordering::Greater,
(None, Some(..)) => Ordering::Less,
(None, None) => Ordering::Equal,
(Some(..), Some(..)) => a.ident.name.cmp(&b.ident.name),
};
Some(result)
}
_ => None,
}
@ -257,7 +276,7 @@ fn rewrite_imports(
false,
);
let mut item_pair_vec: Vec<_> = items.zip(use_items.iter()).collect();
item_pair_vec.sort_by(|a, b| compare_use_items(context, a.1, b.1).unwrap());
item_pair_vec.sort_by(|a, b| compare_use_items(a.1, b.1).unwrap());
let item_vec: Vec<_> = item_pair_vec.into_iter().map(|pair| pair.0).collect();
let fmt = ListFormatting {

View File

@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(match_default_bindings)]
#![feature(rustc_private)]
#![feature(type_ascription)]

View File

@ -1056,27 +1056,16 @@ fn format_derive(context: &RewriteContext, derive_args: &[&str], shape: Shape) -
}
fn is_derive(attr: &ast::Attribute) -> bool {
match attr.meta() {
Some(meta_item) => match meta_item.node {
ast::MetaItemKind::List(..) => meta_item.name.as_str() == "derive",
_ => false,
},
_ => false,
}
attr.check_name("derive")
}
/// Returns the arguments of `#[derive(...)]`.
fn get_derive_args<'a>(context: &'a RewriteContext, attr: &ast::Attribute) -> Option<Vec<&'a str>> {
attr.meta().and_then(|meta_item| match meta_item.node {
ast::MetaItemKind::List(ref args) if meta_item.name.as_str() == "derive" => {
// Every argument of `derive` should be `NestedMetaItemKind::Literal`.
Some(
args.iter()
.map(|a| context.snippet(a.span))
.collect::<Vec<_>>(),
)
}
_ => None,
attr.meta_item_list().map(|meta_item_list| {
meta_item_list
.iter()
.map(|nested_meta_item| context.snippet(nested_meta_item.span))
.collect()
})
}

View File

@ -10,6 +10,10 @@ extern crate chrono;
extern crate foo;
extern crate bar;
// #2315
extern crate proc_macro2;
extern crate proc_macro;
extern "C" {
fn c_func(x: *mut *mut libc::c_void);

View File

@ -1,7 +1,7 @@
// rustfmt-normalize_comments: true
extern crate foo as bar;
extern crate foo;
extern crate foo as bar;
extern crate chrono;
extern crate dotenv;
@ -10,6 +10,10 @@ extern crate futures;
extern crate bar;
extern crate foo;
// #2315
extern crate proc_macro;
extern crate proc_macro2;
extern "C" {
fn c_func(x: *mut *mut libc::c_void);