@@ -863,7 +863,9 @@ object OrganizeImports {
863
863
scalacOptions : List [String ],
864
864
scalaVersion : String
865
865
): Configured [Rule ] = {
866
- val hasCompilerSupport = scalaVersion.startsWith(" 2" )
866
+ val hasCompilerSupport =
867
+ Seq (" 3.0" , " 3.1" , " 3.2" , " 3.3" )
868
+ .forall(v => ! scalaVersion.startsWith(v))
867
869
868
870
val hasWarnUnused = hasCompilerSupport && {
869
871
val warnUnusedPrefix = Set (" -Wunused" , " -Ywarn-unused" )
@@ -911,17 +913,17 @@ object OrganizeImports {
911
913
)
912
914
else if (hasCompilerSupport)
913
915
Configured .error(
914
- " The Scala compiler option \" -Ywarn-unused \" is required to use OrganizeImports with"
916
+ " A Scala compiler option is required to use OrganizeImports with"
915
917
+ " \" OrganizeImports.removeUnused\" set to true. To fix this problem, update your"
916
- + " build to use at least one Scala compiler option like -Ywarn-unused-import (2.11 "
917
- + " only), -Ywarn-unused, -Xlint:unused (2.12.2 or above) or -Wunused (2.13 only )."
918
+ + " build to add ` -Ywarn-unused` (2.12), `-Wunused:imports` (2.13), or "
919
+ + " `-Wunused:import` (3.4+ )."
918
920
)
919
921
else
920
922
Configured .error(
921
- " \" OrganizeImports.removeUnused\" is not supported on Scala 3 as the compiler is"
922
- + " not providing enough information. Run the rule with "
923
- + " \" OrganizeImports.removeUnused\" set to false to organize imports while keeping "
924
- + " potentially unused imports."
923
+ " \" OrganizeImports.removeUnused\" " + s " is not supported on $scalaVersion as the compiler is "
924
+ + " not providing enough information. Please upgrade the Scala compiler to 3.4.0 or greater. "
925
+ + " Otherwise, run the rule with \" OrganizeImports.removeUnused\" set to false"
926
+ + " to organize imports while keeping potentially unused imports."
925
927
)
926
928
}
927
929
@@ -1105,12 +1107,24 @@ object OrganizeImports {
1105
1107
class UnusedImporteePositions (implicit doc : SemanticDocument ) {
1106
1108
private val positions : Seq [Position ] =
1107
1109
doc.diagnostics.toSeq.collect {
1108
- case d if d.message == " Unused import" => d.position
1110
+ // Scala2 says "Unused import" while Scala3 says "unused import"
1111
+ case d if d.message.toLowerCase == " unused import" => d.position
1109
1112
}
1110
1113
1111
1114
/** Returns true if the importee was marked as unused by the compiler */
1112
- def apply (importee : Importee ): Boolean =
1113
- positions contains positionOf(importee)
1115
+ def apply (importee : Importee ): Boolean = {
1116
+ // positionOf returns the position of `bar` for `import foo.{bar => baz}`
1117
+ // this position matches with the diagnostics from Scala2, but Scala3
1118
+ // diagnostics has a position for `bar => baz`, which doesn't match
1119
+ // with the return value of `positionOf`.
1120
+ // We could adjust the behavior of `positionOf` based on Scala version,
1121
+ // but this implementation just checking the unusedImporteePosition
1122
+ // includes the importee pos, for simplicity.
1123
+ val pos = positionOf(importee)
1124
+ positions.exists { unused =>
1125
+ unused.start <= pos.start && pos.end <= unused.end
1126
+ }
1127
+ }
1114
1128
}
1115
1129
1116
1130
implicit private class SymbolExtension (symbol : Symbol ) {
0 commit comments