Skip to content

Commit fa1d2bb

Browse files
committed
Fix -Wunused:import enum
- Some enum generates an import clause for its cases - Update some test suits
1 parent 8635479 commit fa1d2bb

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ object CheckUnused:
331331
registerUsed(sym.owner, None) // constructor are "implicitly" imported with the class
332332
else
333333
usedInScope.top += ((sym, sym.isAccessibleAsIdent, name))
334+
usedInScope.top += ((sym.companionModule, sym.isAccessibleAsIdent, name))
335+
usedInScope.top += ((sym.companionClass, sym.isAccessibleAsIdent, name))
334336

335337
/** Register a symbol that should be ignored */
336338
def addIgnoredUsage(sym: Symbol)(using Context): Unit =
@@ -347,7 +349,7 @@ object CheckUnused:
347349

348350
/** Register an import */
349351
def registerImport(imp: tpd.Import)(using Context): Unit =
350-
if !tpd.languageImport(imp.expr).nonEmpty then
352+
if !tpd.languageImport(imp.expr).nonEmpty && !imp.isGeneratedByEnum then
351353
impInScope.top += imp
352354
unusedImport ++= imp.selectors.filter { s =>
353355
!shouldSelectorBeReported(imp, s) && !isImportExclusion(s)
@@ -591,6 +593,10 @@ object CheckUnused:
591593
!isSyntheticMainParam(sym) &&
592594
!sym.shouldNotReportParamOwner
593595

596+
extension (imp: tpd.Import)
597+
/** Enum generate an import for its cases (but outside them), which should be ignored */
598+
def isGeneratedByEnum(using Context): Boolean =
599+
imp.symbol.exists && imp.symbol.owner.is(Flags.Enum, butNot = Flags.Case)
594600

595601
extension (thisName: Name)
596602
private def isWildcard: Boolean =

tests/neg-custom-args/fatal-warnings/i15503a.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,9 @@ package foo.testing.rename.imports:
250250
package foo.testing.imports.precedence:
251251
import scala.collection.immutable.{BitSet => _, _} // error
252252
import scala.collection.immutable.BitSet // OK
253-
def t = BitSet.empty
253+
def t = BitSet.empty
254+
255+
package foo.test.enums:
256+
enum A: // OK
257+
case B extends A // OK
258+
case C extends A // OK

tests/neg-custom-args/fatal-warnings/i15503i.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,12 @@ package foo.test.scala.annotation:
6565
@unused private def c = 3 // OK
6666

6767
def other = b
68+
69+
package foo.test.companionprivate:
70+
class A:
71+
import A.b // OK
72+
def a = b // OK
73+
74+
object A:
75+
private def b = c // OK
76+
def c = List(1,2,3) // OK

0 commit comments

Comments
 (0)