Repository: intellij-rust/intellij-rust
Issue: Formatter changes order of items and breaks code

```rust
#![allow(non_snake_case)]

extern crate gtk;

use gtk::prelude::*;
use gtk::{ButtonsType, DialogFlags, MessageType, MessageDialog, Window};

fn main() {
    if gtk::init().is_err() {
        println!("Failed to initialize GTK.");
        return;
    }

    MessageDialog::new(None::<&Window>,
                       DialogFlags::empty(),
                       MessageType::Info,
                       ButtonsType::Ok,
                       "Hello World").run();
}
```

gets formatted to

```rust
// Order was changed
use gtk::{ButtonsType, DialogFlags, MessageDialog, MessageType, Window};
use gtk::prelude::*;

// Space before ! was added, which breaks code
# ![allow(non_snake_case)]

extern crate gtk;

fn main() {
    if gtk::init().is_err() {
        println!("Failed to initialize GTK.");
        return;
    }

    MessageDialog::new(None::<&Window>,
                       DialogFlags::empty(),
                       MessageType::Info,
                       ButtonsType::Ok,
                       "Hello World").run();
}
```

version 0.2.0-2275-nightly

Changed files:
src/main/kotlin/org/rust/lang/refactoring/RsImportOptimizer.kt, src/test/kotlin/org/rust/lang/refactoring/RsImportOptimizerTest.kt

diff --git a/src/main/kotlin/org/rust/lang/refactoring/RsImportOptimizer.kt b/src/main/kotlin/org/rust/lang/refactoring/RsImportOptimizer.kt
index 895f9c462..a98de2012 100644
--- a/src/main/kotlin/org/rust/lang/refactoring/RsImportOptimizer.kt
+++ b/src/main/kotlin/org/rust/lang/refactoring/RsImportOptimizer.kt
@@ -55,7 +55,7 @@ class RsImportOptimizer : ImportOptimizer {
 
         private fun replaceOrder(file: RsMod, uses: Collection<RsUseItem>) {
             val first = file.childrenOfType<RsElement>()
-                .firstOrNull { it !is RsExternCrateItem } ?: return
+                .firstOrNull { it !is RsExternCrateItem && it !is RsInnerAttr } ?: return
             val psiFactory = RsPsiFactory(file.project)
             val sortedUses = uses
                 .sortedBy { it.useSpeck?.pathText }
diff --git a/src/test/kotlin/org/rust/lang/refactoring/RsImportOptimizerTest.kt b/src/test/kotlin/org/rust/lang/refactoring/RsImportOptimizerTest.kt
index e9932c707..89d34b11e 100644
--- a/src/test/kotlin/org/rust/lang/refactoring/RsImportOptimizerTest.kt
+++ b/src/test/kotlin/org/rust/lang/refactoring/RsImportOptimizerTest.kt
@@ -16,7 +16,7 @@ class RsImportOptimizerTest: RsTestBase() {
         use foo;
     """)
 
-    fun `test should be at the beginnging`() = doTest("""
+    fun `test should be at the beginning`() = doTest("""
         //! test
         extern crate log;
 
@@ -32,6 +32,34 @@ class RsImportOptimizerTest: RsTestBase() {
 
     """)
 
+    fun `test should be after inner attributes 1`() = doTest("""
+        #![allow(non_snake_case)]
+        extern crate log;
+
+        use foo;
+        use bar;
+    """, """
+        #![allow(non_snake_case)]
+        extern crate log;
+
+        use bar;
+        use foo;
+
+    """)
+
+    fun `test should be after inner attributes 2`() = doTest("""
+        #![allow(non_snake_case)]
+
+        use foo;
+        use bar;
+    """, """
+        #![allow(non_snake_case)]
+
+        use bar;
+        use foo;
+
+    """)
+
     fun `test sort alphabetical of useItem`() = doTest("""
         use foo;
         use bar;
@@ -138,8 +166,7 @@ class RsImportOptimizerTest: RsTestBase() {
     """, """
         #[macro_use]
         pub use /*comment*/ std::mem;
-    """
-    )
+    """)
 
     private fun doTest(@Language("Rust") code: String, @Language("Rust") excepted: String){
         checkByText(code.trimIndent(), excepted.trimIndent()) {