Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix -Wunused:import registering constructor ("<init>") instead of its owner (also fix false positive for enum) #16661

Merged
merged 3 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import dotty.tools.dotc.core.Definitions
import dotty.tools.dotc.core.Types.ConstantType
import dotty.tools.dotc.core.NameKinds.WildcardParamName
import dotty.tools.dotc.core.Types.TermRef
import dotty.tools.dotc.core.Types.NameFilter



Expand Down Expand Up @@ -326,9 +327,12 @@ object CheckUnused:
*/
def registerUsed(sym: Symbol, name: Option[Name])(using Context): Unit =
if !isConstructorOfSynth(sym) && !doNotRegister(sym) then
usedInScope.top += ((sym, sym.isAccessibleAsIdent, name))
if sym.isConstructor && sym.exists then
registerUsed(sym.owner, None) // constructor are "implicitly" imported with the class
else
usedInScope.top += ((sym, sym.isAccessibleAsIdent, name))
usedInScope.top += ((sym.companionModule, sym.isAccessibleAsIdent, name))
usedInScope.top += ((sym.companionClass, sym.isAccessibleAsIdent, name))

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

/** Register an import */
def registerImport(imp: tpd.Import)(using Context): Unit =
if !tpd.languageImport(imp.expr).nonEmpty then
if !tpd.languageImport(imp.expr).nonEmpty && !imp.isGeneratedByEnum then
impInScope.top += imp
unusedImport ++= imp.selectors.filter { s =>
!shouldSelectorBeReported(imp, s) && !isImportExclusion(s)
Expand Down Expand Up @@ -589,6 +593,10 @@ object CheckUnused:
!isSyntheticMainParam(sym) &&
!sym.shouldNotReportParamOwner

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

extension (thisName: Name)
private def isWildcard: Boolean =
Expand Down
7 changes: 6 additions & 1 deletion tests/neg-custom-args/fatal-warnings/i15503a.scala
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,9 @@ package foo.testing.rename.imports:
package foo.testing.imports.precedence:
import scala.collection.immutable.{BitSet => _, _} // error
import scala.collection.immutable.BitSet // OK
def t = BitSet.empty
def t = BitSet.empty

package foo.test.enums:
enum A: // OK
case B extends A // OK
case C extends A // OK
9 changes: 9 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i15503i.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ package foo.test.scala.annotation:
@unused private def c = 3 // OK

def other = b

package foo.test.companionprivate:
class A:
import A.b // OK
def a = b // OK

object A:
private def b = c // OK
def c = List(1,2,3) // OK