]> git.lizzy.rs Git - rust.git/commitdiff
Remove `self` from `use foo::bar::self;`
authorNick Cameron <ncameron@mozilla.com>
Fri, 6 Jan 2017 04:02:56 +0000 (17:02 +1300)
committerNick Cameron <ncameron@mozilla.com>
Fri, 6 Jan 2017 04:02:56 +0000 (17:02 +1300)
Also adds the `normalize_imports` config option.

Fixes #1252

src/config.rs
src/imports.rs
tests/source/imports.rs
tests/target/imports.rs

index 7ee8e4a9097c3a49694d91136c1fb973a243d163..641b8993fc59f5ae971228959e4b359d2fedb3a5 100644 (file)
@@ -393,6 +393,7 @@ fn default() -> Config {
     reorder_imports: bool, false, "Reorder import statements alphabetically";
     reorder_imported_names: bool, false,
         "Reorder lists of names in import statements alphabetically";
+    normalize_imports: bool, true, "Allows removing braces from imports and reducing paths";
     single_line_if_else_max_width: usize, 50, "Maximum line length for single line if-else \
                                                 expressions. A value of zero means always break \
                                                 if-else expressions.";
index d1c362994541880eb7e73eca51652af6355c4909..b2909cfecd263dd0f7386d108a91667062277e85 100644 (file)
@@ -133,15 +133,24 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
             ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
                 rewrite_use_list(width, offset, path, path_list, self.span, context)
             }
-            ast::ViewPath_::ViewPathGlob(_) => {
-                // FIXME convert to list?
-                None
-            }
+            ast::ViewPath_::ViewPathGlob(_) => None,
             ast::ViewPath_::ViewPathSimple(ident, ref path) => {
                 let ident_str = ident.to_string();
                 // 4 = " as ".len()
                 let budget = try_opt!(width.checked_sub(ident_str.len() + 4));
-                let path_str = try_opt!(rewrite_path(context, false, None, path, budget, offset));
+
+                let path_str = if context.config.normalize_imports &&
+                                  path.segments.last().unwrap().identifier.to_string() == "self" &&
+                                  path.segments.len() > 1 {
+                    let path = &ast::Path {
+                        span: path.span.clone(),
+                        segments: path.segments[..path.segments.len() - 1].to_owned(),
+                        global: path.global,
+                    };
+                    try_opt!(rewrite_path(context, false, None, &path, budget, offset))
+                } else {
+                    try_opt!(rewrite_path(context, false, None, path, budget, offset))
+                };
 
                 Some(if path.segments.last().unwrap().identifier == ident {
                     path_str
@@ -239,9 +248,13 @@ pub fn format_import(&mut self, vis: &ast::Visibility, vp: &ast::ViewPath, span:
     }
 }
 
-fn rewrite_single_use_list(path_str: Option<String>, vpi: &ast::PathListItem) -> String {
+fn rewrite_single_use_list(path_str: Option<String>,
+                           vpi: &ast::PathListItem,
+                           context: &RewriteContext)
+                           -> String {
     let path_item_str = match path_str {
-        Some(ref path_str) if vpi.node.name.to_string() == "self" => path_str.to_owned(),
+        Some(ref path_str) if vpi.node.name.to_string() == "self" &&
+                              context.config.normalize_imports => path_str.to_owned(),
         Some(path_str) => format!("{}::{}", path_str, vpi.node.name),
         None => vpi.node.name.to_string(),
     };
@@ -281,7 +294,7 @@ pub fn rewrite_use_list(width: usize,
 
     match path_list.len() {
         0 => unreachable!(),
-        1 => return Some(rewrite_single_use_list(opt_path_str, &path_list[0])),
+        1 => return Some(rewrite_single_use_list(opt_path_str, &path_list[0], context)),
         _ => (),
     }
 
index 45ed1e03fe0e845bcf5cc5aa2527fc5a52850052..4296a152f4e59d91663e536f45ca4e1754b22810 100644 (file)
 pub use syntax::ast::{Expr_, Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath};
 use syntax::some::{};
 
+use self;
+use std::io::{self};
+use std::io::self;
+
 mod Foo {
     pub use syntax::ast::{
         ItemForeignMod,
index 4e2f690364471466e9a7deb716423163fc4918d4..0be8f2c35e0b2decb9d51c9a1c386b098bd96007 100644 (file)
 use Foo::{Bar, Baz};
 pub use syntax::ast::{Expr_, Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath};
 
+use self;
+use std::io;
+use std::io;
+
 mod Foo {
     pub use syntax::ast::{ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, ItemDefaultImpl};