@@ -96,7 +96,8 @@ class OrganizeImports(
96
96
97
97
private def fixWithImplicitDialect (implicit doc : SemanticDocument ): Patch = {
98
98
unusedImporteePositions ++= doc.diagnostics.collect {
99
- case d if d.message == " Unused import" => d.position
99
+ // Scala2 says "Unused import" while Scala3 says "unused import"
100
+ case d if d.message.toLowerCase == " unused import" => d.position
100
101
}
101
102
102
103
val (globalImports, localImports) = collectImports(doc.tree)
@@ -112,8 +113,18 @@ class OrganizeImports(
112
113
diagnostics.map(Patch .lint).asPatch + globalImportsPatch + localImportsPatch
113
114
}
114
115
115
- private def isUnused (importee : Importee ): Boolean =
116
- unusedImporteePositions contains positionOf(importee)
116
+ private def isUnused (importee : Importee ): Boolean = {
117
+ // positionOf returns the position of `bar` for `import foo.{bar => baz}`
118
+ // this position matches with the diagnostics from Scala2,
119
+ // but Scala3 diagnostics has a position for `bar => baz`, which doesn't match with
120
+ // the return value of `positionOf`.
121
+ // We could adjust the behavior of `positionOf` based on Scala version,
122
+ // but this implementation just checking the unusedImporteePosition includes the importee pos, for simplicity.
123
+ val pos = positionOf(importee)
124
+ unusedImporteePositions.exists(unused =>
125
+ unused.start <= pos.start && pos.end <= unused.end
126
+ )
127
+ }
117
128
118
129
private def organizeGlobalImports (
119
130
imports : Seq [Import ]
@@ -839,7 +850,9 @@ object OrganizeImports {
839
850
scalacOptions : List [String ],
840
851
scalaVersion : String
841
852
): Configured [Rule ] = {
842
- val hasCompilerSupport = scalaVersion.startsWith(" 2" )
853
+ val hasCompilerSupport =
854
+ Seq (" 3.0" , " 3.1" , " 3.2" , " 3.3" )
855
+ .forall(v => ! scalaVersion.startsWith(v))
843
856
844
857
val hasWarnUnused = hasCompilerSupport && {
845
858
val warnUnusedPrefix = Set (" -Wunused" , " -Ywarn-unused" )
@@ -887,17 +900,17 @@ object OrganizeImports {
887
900
)
888
901
else if (hasCompilerSupport)
889
902
Configured .error(
890
- " The Scala compiler option \" -Ywarn-unused \" is required to use OrganizeImports with"
903
+ " A Scala compiler option is required to use OrganizeImports with"
891
904
+ " \" OrganizeImports.removeUnused\" set to true. To fix this problem, update your"
892
- + " build to use at least one Scala compiler option like -Ywarn-unused-import (2.11 "
893
- + " only), -Ywarn-unused, -Xlint:unused (2.12.2 or above) or -Wunused (2.13 only )."
905
+ + " build to add ` -Ywarn-unused` (2.12), `-Wunused:imports` (2.13), or "
906
+ + " `-Wunused:import` (3.4+ )."
894
907
)
895
908
else
896
909
Configured .error(
897
- " \" OrganizeImports.removeUnused\" is not supported on Scala 3 as the compiler is"
898
- + " not providing enough information. Run the rule with "
899
- + " \" OrganizeImports.removeUnused\" set to false to organize imports while keeping "
900
- + " potentially unused imports."
910
+ " \" OrganizeImports.removeUnused\" " + s " is not supported on $scalaVersion as the compiler is "
911
+ + " not providing enough information. Please upgrade the Scala compiler to 3.4.0 or greater. "
912
+ + " Otherwise, run the rule with \" OrganizeImports.removeUnused\" set to false"
913
+ + " to organize imports while keeping potentially unused imports."
901
914
)
902
915
}
903
916
0 commit comments