Repository: intellij-rust/intellij-rust
Issue: Optimize Imports action may produce incorrect code

<!--
Hello and thank you for the issue!
If you would like to report a bug, we have added some points below that you can fill out.
Feel free to remove all the irrelevant text to request a new feature.
-->

## Environment

* **Intellij-Rust plugin version:** 0.2.0.2437-183-nightly
* **Rust toolchain version:** 1.29.1
* **IDE name and version:** IntelliJ IDEA Community 2018.3 EAP
* **Operating system:** macOS High Sierra 10.13.6

## Problem description

The `Optimize Imports` action may produce incorrect code. For example, it transforms this code

```rust
mod a {
    pub mod b {
        use std::time;
        const ONE_SECOND: time::Duration = time::Duration::from_millis(1000);
    }
}
```
to this
```rust
mod a {
    use std::time;pub mod b {
        const ONE_SECOND: time::Duration = time::Duration::from_millis(1000);
    }
}
```
(`time::Duration` and `time::Duration::from_millis` not in scope).
## Steps to reproduce

* Copy-paste code from problem description.


* Run `Optimize Imports` action.

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 860041e82..6bd1abf09 100644
--- a/src/main/kotlin/org/rust/lang/refactoring/RsImportOptimizer.kt
+++ b/src/main/kotlin/org/rust/lang/refactoring/RsImportOptimizer.kt
@@ -66,10 +66,12 @@ class RsImportOptimizer : ImportOptimizer {
             return true
         }
 
-        private fun replaceOrderOfUseItems(file: RsMod, uses: Collection<RsUseItem>) {
-            val first = file.childrenOfType<RsElement>()
-                .firstOrNull { it !is RsExternCrateItem && it !is RsAttr } ?: return
-            val psiFactory = RsPsiFactory(file.project)
+        private fun replaceOrderOfUseItems(mod: RsMod, uses: Collection<RsUseItem>) {
+            // We should ignore all items before `{` in inline modules
+            val offset = if (mod is RsModItem) mod.lbrace.textOffset + 1 else 0
+            val first = mod.childrenOfType<RsElement>()
+                .firstOrNull { it.textOffset >= offset && it !is RsExternCrateItem && it !is RsAttr } ?: return
+            val psiFactory = RsPsiFactory(mod.project)
             val sortedUses = uses
                 .sortedBy { it.useSpeck?.pathText }
                 .mapNotNull { it.copy() as? RsUseItem }
@@ -77,7 +79,7 @@ class RsImportOptimizer : ImportOptimizer {
                 .mapNotNull { it.useSpeck }
                 .forEach { optimizeUseSpeck(psiFactory, it) }
             for (importPath in sortedUses) {
-                file.addBefore(importPath, first)
+                mod.addBefore(importPath, first)
             }
             uses.forEach { it.delete() }
         }
diff --git a/src/test/kotlin/org/rust/lang/refactoring/RsImportOptimizerTest.kt b/src/test/kotlin/org/rust/lang/refactoring/RsImportOptimizerTest.kt
index e5bbab710..26135536c 100644
--- a/src/test/kotlin/org/rust/lang/refactoring/RsImportOptimizerTest.kt
+++ b/src/test/kotlin/org/rust/lang/refactoring/RsImportOptimizerTest.kt
@@ -220,6 +220,18 @@ class RsImportOptimizerTest: RsTestBase() {
         }
     """)
 
+    fun `test do not move use items before mod block`() = doTest("""
+        pub mod foo {
+            use std::time;
+            use std::io;
+        }
+    """, """
+        pub mod foo {
+            use std::io;
+            use std::time;
+        }
+    """)
+
     private fun doTest(@Language("Rust") code: String, @Language("Rust") excepted: String){
         checkByText(code.trimIndent(), excepted.trimIndent()) {
             myFixture.performEditorAction("OptimizeImports")