From 3377ba4dd76d458bb7746379dad5df2c70ad1cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fausto=20N=C3=BA=C3=B1ez=20Alberro?= <fausto.nunez@mailbox.org> Date: Tue, 25 Jul 2017 19:46:09 +0200 Subject: [PATCH] Remove whitespace between extern crate declaration --- src/visitor.rs | 10 +++++++++- tests/source/extern.rs | 3 +++ tests/target/extern.rs | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/visitor.rs b/src/visitor.rs index 0f7005e2856..2326848ddd2 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -25,6 +25,7 @@ use items::{format_impl, format_trait, rewrite_associated_impl_type, rewrite_ass rewrite_static, rewrite_type_alias}; use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorTactic}; use macros::{rewrite_macro, MacroPosition}; +use regex::Regex; use rewrite::{Rewrite, RewriteContext}; use utils::{self, contains_skip, mk_sp}; @@ -338,7 +339,14 @@ impl<'a> FmtVisitor<'a> { ast::ItemKind::ExternCrate(_) => { self.format_missing_with_indent(source!(self, item.span).lo); let new_str = self.snippet(item.span); - self.buffer.push_str(&new_str); + if contains_comment(&new_str) { + self.buffer.push_str(&new_str) + } else { + let no_whitespace = + &new_str.split_whitespace().collect::<Vec<&str>>().join(" "); + self.buffer + .push_str(&Regex::new(r"\s;").unwrap().replace(no_whitespace, ";")); + } self.last_pos = source!(self, item.span).hi; } ast::ItemKind::Struct(ref def, ref generics) => { diff --git a/tests/source/extern.rs b/tests/source/extern.rs index 1dd1df2b74e..7f14d27b779 100644 --- a/tests/source/extern.rs +++ b/tests/source/extern.rs @@ -1,5 +1,8 @@ // rustfmt-normalize_comments: true + extern crate foo ; + extern crate foo as bar ; + extern "C" { fn c_func(x: *mut *mut libc::c_void); diff --git a/tests/target/extern.rs b/tests/target/extern.rs index efee6ab26bf..1431c384db9 100644 --- a/tests/target/extern.rs +++ b/tests/target/extern.rs @@ -1,5 +1,8 @@ // rustfmt-normalize_comments: true +extern crate foo; +extern crate foo as bar; + extern "C" { fn c_func(x: *mut *mut libc::c_void);