Skip to content

Commit 7de90b3

Browse files
committed
Add better support for trivial methods -Wunused
- Method having a return type of "SingleType" (as named in Scala 2) are considered trivial (i.e. Nil, object X, object Y, None, ...) - Update test suits
1 parent c70fa68 commit 7de90b3

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import dotty.tools.dotc.core.Annotations
2626
import dotty.tools.dotc.core.Definitions
2727
import dotty.tools.dotc.core.Types.ConstantType
2828
import dotty.tools.dotc.core.NameKinds.WildcardParamName
29+
import dotty.tools.dotc.core.Types.TermRef
2930

3031

3132

@@ -569,7 +570,13 @@ object CheckUnused:
569570
defdef.symbol.is(Deferred) ||
570571
(rhs match {
571572
case _: tpd.Literal => true
572-
case _ => rhs.tpe.isInstanceOf[ConstantType]
573+
case _ => rhs.tpe match
574+
case ConstantType(_) => true
575+
case tp: TermRef =>
576+
// Detect Scala 2 SingleType
577+
tp.underlying.classSymbol.is(Flags.Module)
578+
case _ =>
579+
false
573580
})
574581
def registerTrivial(using Context): Unit =
575582
if defdef.isTrivial then
@@ -590,7 +597,6 @@ object CheckUnused:
590597
private def isWildcard: Boolean =
591598
thisName == StdNames.nme.WILDCARD || thisName.is(WildcardParamName)
592599

593-
594600
end UnusedData
595601

596602
private object UnusedData:

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,31 @@ package scala2main:
2424
package scala3main:
2525
/* This goes around the "trivial method" detection */
2626
val default_unit = ()
27-
@main def hello = println("Hello World") // OK
27+
@main def hello = println("Hello World") // OK
28+
29+
package foo.test.lambda.param:
30+
val default_val = 1
31+
val a = (i: Int) => i // OK
32+
val b = (i: Int) => default_val // error
33+
val c = (_: Int) => default_val // OK
34+
35+
package foo.test.trivial:
36+
/* A twisted test from Scala 2 */
37+
class C {
38+
def answer: 42 = 42
39+
object X
40+
def g0(x: Int) = ??? // OK
41+
def f0(x: Int) = () // OK
42+
def f1(x: Int) = throw new RuntimeException // OK
43+
def f2(x: Int) = 42 // OK
44+
def f3(x: Int): Option[Int] = None // OK
45+
def f4(x: Int) = classOf[Int] // OK
46+
def f5(x: Int) = answer + 27 // OK
47+
def f6(x: Int) = X // OK
48+
def f7(x: Int) = Y // OK
49+
def f8(x: Int): List[C] = Nil // OK
50+
def f9(x: Int): List[Int] = List(1,2,3,4) // error
51+
def foo:Int = 32 // OK
52+
def f77(x: Int) = foo // error
53+
}
54+
object Y

0 commit comments

Comments
 (0)