Skip to content

Fix #16675 : -Wunused false positive on case class generated method, due to flags used to distinguish case accessors. #16683

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

Merged
merged 1 commit into from
Jan 13, 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
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ object CheckUnused:
explicitParamInScope += memDef
else if currScopeType.top == ScopeType.Local then
localDefInScope += memDef
else if currScopeType.top == ScopeType.Template && memDef.symbol.is(Private, butNot = SelfName) then
else if memDef.shouldReportPrivateDef then
privateDefInScope += memDef

/** Register pattern variable */
Expand Down Expand Up @@ -589,10 +589,13 @@ object CheckUnused:

private def isValidParam(using Context): Boolean =
val sym = memDef.symbol
(sym.is(Param) || sym.isAllOf(PrivateParamAccessor)) &&
(sym.is(Param) || sym.isAllOf(PrivateParamAccessor | Local, butNot = CaseAccessor)) &&
!isSyntheticMainParam(sym) &&
!sym.shouldNotReportParamOwner

private def shouldReportPrivateDef(using Context): Boolean =
currScopeType.top == ScopeType.Template && !memDef.symbol.isConstructor && memDef.symbol.is(Private, butNot = SelfName | Synthetic | CaseAccessor)

extension (imp: tpd.Import)
/** Enum generate an import for its cases (but outside them), which should be ignored */
def isGeneratedByEnum(using Context): Boolean =
Expand Down
9 changes: 8 additions & 1 deletion tests/neg-custom-args/fatal-warnings/i15503c.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ class A:

val x = 1 // OK
def y = 2 // OK
def z = g // OK
def z = g // OK

package foo.test.contructors:
case class A private (x:Int) // OK
class B private (val x: Int) // OK
class C private (private val x: Int) // error
class D private (private val x: Int): // OK
def y = x
49 changes: 48 additions & 1 deletion tests/neg-custom-args/fatal-warnings/i15503i.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,51 @@ package foo.test.companionprivate:

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

package foo.test.possibleclasses:
case class AllCaseClass(
k: Int, // OK
private val y: Int // OK /* Kept as it can be taken from pattern */
)(
s: Int, // error /* But not these */
val t: Int, // OK
private val z: Int // error
)

case class AllCaseUsed(
k: Int, // OK
private val y: Int // OK
)(
s: Int, // OK
val t: Int, // OK
private val z: Int // OK
) {
def a = k + y + s + t + z
}

class AllClass(
k: Int, // error
private val y: Int // error
)(
s: Int, // error
val t: Int, // OK
private val z: Int // error
)

class AllUsed(
k: Int, // OK
private val y: Int // OK
)(
s: Int, // OK
val t: Int, // OK
private val z: Int // OK
) {
def a = k + y + s + t + z
}

package foo.test.from.i16675:
case class PositiveNumber private (i: Int) // OK
object PositiveNumber:
def make(i: Int): Option[PositiveNumber] = //OK
Option.when(i >= 0)(PositiveNumber(i)) // OK