Repository: intellij-rust/intellij-rust
Issue: "Optimize Imports" removes `#[allow(unused_imports)]` imports

<!--
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.4.184.5057-222
* **Rust toolchain version:** 1.67.0-nightly (77e57db38 2022-10-30) x86_64-pc-windows-msvc
* **IDE name and version:** PyCharm 2022.2.4 Community Edition (PC-222.4459.20)
* **Operating system:** Windows 10 10.0
* **Macro expansion:** enabled
* **Additional experimental features:** org.rust.macros.proc

## Problem description
In the embedded space it is common to import a panic handler crate. 
This import is "unused" but required for the `#[panic_handler]` to be registered. 

It would be nice if "Optimize Imports" could ignore imports marked with `#[allow(unused_imports)]`

## Steps to reproduce
```rust
#[allow(unused_imports)]
use panic_halt as _;
```
"Code | Optimize Imports" 

<!--
Please include as much of your codebase as needed to reproduce the error.
If the relevant files are large, please provide a link to a public repository or a [Gist](https://gist.github.com/).
-->

Changed files:
src/main/kotlin/org/rust/ide/inspections/lints/RsUnusedImportInspection.kt, src/main/kotlin/org/rust/ide/inspections/lints/RsLint.kt, src/test/kotlin/org/rust/ide/refactoring/RsImportOptimizerTest.kt

diff --git a/src/main/kotlin/org/rust/ide/inspections/lints/RsLint.kt b/src/main/kotlin/org/rust/ide/inspections/lints/RsLint.kt
index 819043630..2ac096a17 100644
--- a/src/main/kotlin/org/rust/ide/inspections/lints/RsLint.kt
+++ b/src/main/kotlin/org/rust/ide/inspections/lints/RsLint.kt
@@ -46,7 +46,7 @@ enum class RsLint(
      */
     fun levelFor(el: PsiElement): RsLintLevel = explicitLevel(el) ?: superModsLevel(el) ?: defaultLevel
 
-    private fun explicitLevel(el: PsiElement): RsLintLevel? = el.ancestors
+    fun explicitLevel(el: PsiElement): RsLintLevel? = el.ancestors
         .filterIsInstance<RsDocAndAttributeOwner>()
         .flatMap { it.queryAttributes.metaItems.toList().asReversed().asSequence() }
         .filter { it.metaItemArgs?.metaItemList.orEmpty().any { item -> item.id == id || item.id in groupIds } }
diff --git a/src/main/kotlin/org/rust/ide/inspections/lints/RsUnusedImportInspection.kt b/src/main/kotlin/org/rust/ide/inspections/lints/RsUnusedImportInspection.kt
index 1f0eb7727..cf6d1a585 100644
--- a/src/main/kotlin/org/rust/ide/inspections/lints/RsUnusedImportInspection.kt
+++ b/src/main/kotlin/org/rust/ide/inspections/lints/RsUnusedImportInspection.kt
@@ -153,6 +153,7 @@ fun RsUseSpeck.isUsed(pathUsage: PathUsageMap): Boolean {
     val useItem = ancestorStrict<RsUseItem>() ?: return true
     if (!isApplicableForUseItem(useItem)) return true
     return isUseSpeckUsed(this, pathUsage)
+        || RsLint.UnusedImports.explicitLevel(useItem) == RsLintLevel.ALLOW
 }
 
 private fun isUseSpeckUsed(useSpeck: RsUseSpeck, usage: PathUsageMap): Boolean {
diff --git a/src/test/kotlin/org/rust/ide/refactoring/RsImportOptimizerTest.kt b/src/test/kotlin/org/rust/ide/refactoring/RsImportOptimizerTest.kt
index fcca2495c..30244a05e 100644
--- a/src/test/kotlin/org/rust/ide/refactoring/RsImportOptimizerTest.kt
+++ b/src/test/kotlin/org/rust/ide/refactoring/RsImportOptimizerTest.kt
@@ -660,6 +660,15 @@ class RsImportOptimizerTest: RsTestBase() {
         }
     """)
 
+    fun `test keep unused import with allow lint`() = checkNotChanged("""
+        #[allow(unused_imports)]
+        use foo::S;
+
+        mod foo {
+            pub struct S;
+        }
+    """)
+
     fun `test ignore reexport of legacy macro`() = checkNotChanged("""
         macro_rules! foo1 { () => {} }
         macro_rules! foo2 { () => {} }