From 846f4f21db34b252b5e42c7a841f14624c643280 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 22 Mar 2018 15:55:14 +0900 Subject: [PATCH] Fix libsyntax updates `ast::UseTreeKind::Simple` now takes `Option` instead of `ast::Ident`. --- src/imports.rs | 35 ++++++++++++++++------------------- src/reorder.rs | 12 +++++++----- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/imports.rs b/src/imports.rs index 9a6af3f267c..f631f4b9c26 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -29,6 +29,10 @@ pub fn path_to_imported_ident(path: &ast::Path) -> ast::Ident { path.segments.last().unwrap().identifier } +pub fn same_rename(opt_ident: &Option, path: &ast::Path) -> bool { + opt_ident.map_or(true, |ident| path_to_imported_ident(path) == ident) +} + fn rewrite_prefix(path: &ast::Path, context: &RewriteContext, shape: Shape) -> Option { if path.segments.len() > 1 && path_to_imported_ident(path).to_string() == "self" { let path = &ast::Path { @@ -57,22 +61,16 @@ impl Rewrite for ast::UseTree { Some("*".to_owned()) } } - ast::UseTreeKind::Simple(ident) => { - let ident_str = ident.to_string(); - - // 4 = " as ".len() - let is_same_name_bind = path_to_imported_ident(&self.prefix) == ident; - let prefix_shape = if is_same_name_bind { - shape - } else { - shape.sub_width(ident_str.len() + 4)? - }; - let path_str = rewrite_prefix(&self.prefix, context, prefix_shape) - .unwrap_or_else(|| context.snippet(self.prefix.span).to_owned()); - - if is_same_name_bind { - Some(path_str) + ast::UseTreeKind::Simple(opt_ident) => { + if same_rename(&opt_ident, &self.prefix) { + rewrite_prefix(&self.prefix, context, shape) + .or_else(|| Some(context.snippet(self.prefix.span).to_owned())) } else { + let ident_str = opt_ident?.to_string(); + // 4 = " as ".len() + let prefix_shape = shape.sub_width(ident_str.len() + 4)?; + let path_str = rewrite_prefix(&self.prefix, context, prefix_shape) + .unwrap_or_else(|| context.snippet(self.prefix.span).to_owned()); Some(format!("{} as {}", path_str, ident_str)) } } @@ -161,8 +159,7 @@ fn rewrite_nested_use_tree_single( shape: Shape, ) -> Option { match tree.kind { - ast::UseTreeKind::Simple(rename) => { - let ident = path_to_imported_ident(&tree.prefix); + ast::UseTreeKind::Simple(opt_rename) => { let mut item_str = rewrite_prefix(&tree.prefix, context, shape)?; if item_str == "self" { item_str = "".to_owned(); @@ -180,10 +177,10 @@ fn rewrite_nested_use_tree_single( format!("{}::{}", path_str, item_str) }; - Some(if ident == rename { + Some(if same_rename(&opt_rename, &tree.prefix) { path_item_str } else { - format!("{} as {}", path_item_str, rename) + format!("{} as {}", path_item_str, opt_rename?) }) } ast::UseTreeKind::Glob | ast::UseTreeKind::Nested(..) => { diff --git a/src/reorder.rs b/src/reorder.rs index 5595fa5b237..b216313f3b0 100644 --- a/src/reorder.rs +++ b/src/reorder.rs @@ -321,11 +321,13 @@ impl UseTree { } UseTreeKind::Simple(ref rename) => { let mut name = (*path_to_imported_ident(&a.prefix).name.as_str()).to_owned(); - let alias = if &name == &*rename.name.as_str() { - None - } else { - Some((&*rename.name.as_str()).to_owned()) - }; + let alias = rename.and_then(|ident| { + if ident == path_to_imported_ident(&a.prefix) { + None + } else { + Some(ident.to_string()) + } + }); let segment = if &name == "self" { UseSegment::Slf(alias)