Skip to content

Commit

Permalink
Tighten allowance for serialization methods
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Oct 9, 2024
1 parent 1b77bab commit 6c6bdd4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ object CheckUnused:
peekScopeType == ScopeType.Template
&& !memDef.symbol.isConstructor
&& memDef.symbol.is(Private, butNot = SelfName | Synthetic | CaseAccessor)
&& !ignoredNames(memDef.symbol.name.toTermName)
&& !ignoredSignature(memDef.symbol)

private def isUnsetVarDef(using Context): Boolean =
val sym = memDef.symbol
Expand Down Expand Up @@ -747,7 +747,12 @@ object CheckUnused:
case _: Block => Local
case _ => Other

val ignoredNames: Set[TermName] = Set("readResolve", "readObject", "writeObject", "writeReplace").map(termName(_))
val ignoredNames: Set[TermName] =
Set("readResolve", "readObject", "readObjectNoData", "writeObject", "writeReplace").map(termName(_))

def ignoredSignature(m: Symbol)(using Context): Boolean =
m.is(Method) && ignoredNames(m.name.toTermName) && m.owner.isClass
&& m.owner.asClass.classDenot.parentSyms.contains(defn.JavaSerializableClass)

final case class ImportSelectorData(val qualTpe: Type, val selector: ImportSelector):
private var myUsed: Boolean = false
Expand Down
30 changes: 29 additions & 1 deletion tests/warn/unused-privates.scala
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ trait Forever {
}

trait Ignorance {
private val readResolve = 42 // no warn special members
private val readResolve = 42 // warn wrong signatured for special members
}

trait CaseyKasem {
Expand Down Expand Up @@ -257,3 +257,31 @@ class `recursive reference is not a usage` {
def f() = new P()
}
}

class `absolve serial framework` extends Serializable:
import java.io.{IOException, ObjectInputStream, ObjectOutputStream, ObjectStreamException}
@throws(classOf[IOException])
private def writeObject(stream: ObjectOutputStream): Unit = ()
@throws(classOf[ObjectStreamException])
private def writeReplace(): Object = ???
@throws(classOf[ClassNotFoundException])
@throws(classOf[IOException])
private def readObject(stream: ObjectInputStream): Unit = ()
@throws(classOf[ObjectStreamException])
private def readObjectNoData(): Unit = ()
@throws(classOf[ObjectStreamException])
private def readResolve(): Object = ???

class `absolve ONLY serial framework`:
import java.io.{IOException, ObjectInputStream, ObjectOutputStream, ObjectStreamException}
@throws(classOf[IOException])
private def writeObject(stream: ObjectOutputStream): Unit = () // warn
@throws(classOf[ObjectStreamException])
private def writeReplace(): Object = ??? // warn
@throws(classOf[ClassNotFoundException])
@throws(classOf[IOException])
private def readObject(stream: ObjectInputStream): Unit = () // warn
@throws(classOf[ObjectStreamException])
private def readObjectNoData(): Unit = () // warn
@throws(classOf[ObjectStreamException])
private def readResolve(): Object = ??? // warn

0 comments on commit 6c6bdd4

Please sign in to comment.