Repository: intellij-rust/intellij-rust
Issue: Auto-imports in "include!"-ed file should be added to the included files, not its "parent"

## Environment

* **IntelliJ Rust plugin version:** 0.4.158.4203-212
* **Rust toolchain version:** 1.56.0 (09c42c458 2021-10-18) x86_64-apple-darwin
* **IDE name and version:** CLion 2021.2.2 (CL-212.5284.51)
* **Operating system:** macOS 11.2.3
* **Macro expansion engine:** new
* **Name resolution engine:** new

## Problem description

Auto-imports in "include!"-ed file are added to the "parent" file instead of the file where they are used.

Consider following scenario in multi-platform project:
```rust
// mod.rs

// use ANativeWindow_acquire statement will be added here

#[cfg(target_os = "android")]
include!("mod_android.rs");

#[cfg(target_os = "macos")]
include!("mod_macos.rs");

// mod_android.rs

// use ANativeWindow_acquire statement must be added here

pub fn test() {
  // ANativeWindow_acquire(...) call here
}

// mod_macos.rs

pub fn test() {
}
```

## Steps to reproduce

Consider that I need to call `ANativeWindow_acquire` function from the `ndk-sys` crate in the `test()` function in the `mod_android.rs` file. The actual use statement is added to the `mod.rs` instead of the `mod_andoird.rs`.

This messes up compilation for MacOS, because including `ndk-sys` will throw an error on unsupported platforms. This happens because of the following line:
```rust
#[cfg(not(any(target_os = "android", feature = "test")))]
compile_error!("android-ndk-sys only supports compiling for Android");
```

This problem is not only related to the `ndk-sys` create. There are other crates that have the same issues. Also use statements logically should go to the file where they are used.

Changed files:
src/test/kotlin/org/rust/ide/inspections/import/AutoImportFixTest.kt, src/main/kotlin/org/rust/ide/utils/import/importUtils.kt

diff --git a/src/main/kotlin/org/rust/ide/utils/import/importUtils.kt b/src/main/kotlin/org/rust/ide/utils/import/importUtils.kt
index 550c38f59..b6c6edf28 100644
--- a/src/main/kotlin/org/rust/ide/utils/import/importUtils.kt
+++ b/src/main/kotlin/org/rust/ide/utils/import/importUtils.kt
@@ -13,6 +13,7 @@ import org.rust.lang.core.crate.Crate
 import org.rust.lang.core.macros.setContext
 import org.rust.lang.core.psi.*
 import org.rust.lang.core.psi.ext.*
+import org.rust.lang.core.stubs.index.RsIncludeMacroIndex
 import org.rust.openapiext.Testmark
 import org.rust.openapiext.checkWriteAccessAllowed
 import org.rust.stdext.isSortedWith
@@ -46,6 +47,7 @@ fun ImportCandidateBase.import(context: RsElement) {
             ((scope as? RsFunction)?.block ?: scope) as RsItemsOwner
         }
         containingFile is RsCodeFragment -> containingFile.importTarget
+        containingFile is RsFile && RsIncludeMacroIndex.getIncludedFrom(containingFile) != null -> containingFile
         else -> null
     } ?: context.containingMod
     insertionScope.insertUseItem(psiFactory, "$prefix${info.usePath}")
diff --git a/src/test/kotlin/org/rust/ide/inspections/import/AutoImportFixTest.kt b/src/test/kotlin/org/rust/ide/inspections/import/AutoImportFixTest.kt
index c9c3742bd..d21d544e4 100644
--- a/src/test/kotlin/org/rust/ide/inspections/import/AutoImportFixTest.kt
+++ b/src/test/kotlin/org/rust/ide/inspections/import/AutoImportFixTest.kt
@@ -2955,4 +2955,29 @@ class AutoImportFixTest : AutoImportFixTestBase() {
             }
         }
     """)
+
+    @MockEdition(CargoWorkspace.Edition.EDITION_2018)
+    fun `test import inside included file`() = checkAutoImportFixByFileTree("""
+        //- foo.rs
+        fn func() {
+            <error descr="Unresolved reference: `foo`">foo/*caret*/</error>();
+        }
+        //- lib.rs
+        include!("foo.rs");
+        mod inner {
+            pub fn foo() {}
+        }
+    """, """
+        //- foo.rs
+        use crate::inner::foo;
+
+        fn func() {
+            foo();
+        }
+        //- lib.rs
+        include!("foo.rs");
+        mod inner {
+            pub fn foo() {}
+        }
+    """)
 }