Skip to content

Commit

Permalink
Merge pull request #10305 from som-snytt/issue/10314-unused-from-self…
Browse files Browse the repository at this point in the history
…type

Unused warning checks self-type for overrides
  • Loading branch information
lrytz committed Jul 5, 2023
2 parents 5148ddd + 4008c6c commit 9e7c2d5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import scala.tools.nsc.Reporting.WarningCategory
* @author Paul Phillips
*/
trait TypeDiagnostics extends splain.SplainDiagnostics {
self: Analyzer with StdAttachments =>
_: Analyzer with StdAttachments =>

import global._
import definitions._
Expand Down Expand Up @@ -738,8 +738,11 @@ trait TypeDiagnostics extends splain.SplainDiagnostics {
if (settings.warnUnusedParams) {
def isImplementation(m: Symbol): Boolean = {
def classOf(s: Symbol): Symbol = if (s.isClass || s == NoSymbol) s else classOf(s.owner)
val opc = new overridingPairs.PairsCursor(classOf(m))
opc.iterator.exists(pair => pair.low == m)
val opc = new overridingPairs.PairsCursor(classOf(m)) {
override protected def bases: List[Symbol] = self.baseClasses
override protected def exclude(sym: Symbol) = super.exclude(sym) || sym.name != m.name || sym.paramLists.isEmpty || sym.paramLists.head.isEmpty
}
opc.iterator.exists(pair => pair.low == m || pair.high == m)
}
import PartialFunction._
def isConvention(p: Symbol): Boolean = (
Expand Down Expand Up @@ -780,7 +783,7 @@ trait TypeDiagnostics extends splain.SplainDiagnostics {


trait TyperDiagnostics {
self: Typer =>
_: Typer =>

def permanentlyHiddenWarning(pos: Position, hidden: Name, defn: Symbol) =
context.warning(pos, "imported `%s` is permanently hidden by definition of %s".format(hidden, defn.fullLocationString), WarningCategory.OtherShadowing)
Expand Down
8 changes: 7 additions & 1 deletion test/files/neg/warn-unused-params.check
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ warn-unused-params.scala:102: warning: evidence parameter evidence$1 of type Con
warn-unused-params.scala:104: warning: evidence parameter evidence$2 of type Context[A] in class Bound is never used
class Bound[A: Context]
^
warn-unused-params.scala:111: warning: parameter b in method f is never used
b: String, // warn
^
warn-unused-params.scala:134: warning: parameter s in method i is never used
def i(implicit s: String) = answer // yes, warn
^
error: No warnings can be incurred under -Werror.
13 warnings
15 warnings
1 error
32 changes: 32 additions & 0 deletions test/files/neg/warn-unused-params.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,35 @@ class Bound[A: Context]
object Answers {
def answer: Int = 42
}

trait BadMix { _: InterFace =>
def f(a: Int,
b: String, // warn
c: Double): Int = {
println(c)
a
}
@deprecated("no warn in deprecated API", since="yesterday")
def g(a: Int,
b: String, // no warn
c: Double): Int = {
println(c)
a
}
override def call(a: Int,
b: String, // no warn, required by superclass
c: Double): Int = {
println(c)
a
}

def meth(x: Int) = x

override def equals(other: Any): Boolean = true // no warn

def i(implicit s: String) = answer // yes, warn
}

class Unequal {
override def equals(other: Any) = toString.nonEmpty // no warn non-trivial RHS, required by universal method
}
2 changes: 1 addition & 1 deletion test/files/pos/t12520.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ trait TimeLimitedTests extends TestSuiteMixin { this: TestSuite =>
}

trait AnyFunSuiteLike extends TestSuite
abstract class Test[C] extends AnyFunSuiteLike with TimeLimitedTests
abstract class Test[C] extends AnyFunSuiteLike with TimeLimitedTests

0 comments on commit 9e7c2d5

Please sign in to comment.