Skip to content

Commit 7f04ce3

Browse files
committed
Add support for @annotation.unused
- Does not report defs with @unused - Update the test suit
1 parent b0790d1 commit 7f04ce3

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,7 @@ class Definitions {
991991
@tu lazy val MappedAlternativeAnnot: ClassSymbol = requiredClass("scala.annotation.internal.MappedAlternative")
992992
@tu lazy val MigrationAnnot: ClassSymbol = requiredClass("scala.annotation.migration")
993993
@tu lazy val NowarnAnnot: ClassSymbol = requiredClass("scala.annotation.nowarn")
994+
@tu lazy val UnusedAnnot: ClassSymbol = requiredClass("scala.annotation.unused")
994995
@tu lazy val TransparentTraitAnnot: ClassSymbol = requiredClass("scala.annotation.transparentTrait")
995996
@tu lazy val NativeAnnot: ClassSymbol = requiredClass("scala.native")
996997
@tu lazy val RepeatedAnnot: ClassSymbol = requiredClass("scala.annotation.internal.Repeated")

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import dotty.tools.dotc.core.Flags.flagsString
2222
import dotty.tools.dotc.core.Flags
2323
import dotty.tools.dotc.core.Names.Name
2424
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
25+
import dotty.tools.dotc.core.Annotations
26+
import dotty.tools.dotc.core.Definitions
2527

2628

2729

@@ -307,20 +309,22 @@ object CheckUnused:
307309
def registerDef(valOrDef: tpd.ValOrDefDef)(using Context): Unit =
308310
// register the annotations for usage
309311
registerUsedAnnotation(valOrDef.symbol)
310-
if valOrDef.symbol.is(Param) && !isSyntheticMainParam(valOrDef.symbol) then
311-
if valOrDef.symbol.isOneOf(GivenOrImplicit) then
312-
implicitParamInScope += valOrDef
313-
else
314-
explicitParamInScope += valOrDef
315-
else if currScopeType.top == ScopeType.Local then
316-
localDefInScope += valOrDef
317-
else if currScopeType.top == ScopeType.Template && valOrDef.symbol.is(Private, butNot = SelfName) then
318-
privateDefInScope += valOrDef
312+
if !valOrDef.symbol.isUnusedAnnot then
313+
if valOrDef.symbol.is(Param) && !isSyntheticMainParam(valOrDef.symbol) then
314+
if valOrDef.symbol.isOneOf(GivenOrImplicit) then
315+
implicitParamInScope += valOrDef
316+
else
317+
explicitParamInScope += valOrDef
318+
else if currScopeType.top == ScopeType.Local then
319+
localDefInScope += valOrDef
320+
else if currScopeType.top == ScopeType.Template && valOrDef.symbol.is(Private, butNot = SelfName) then
321+
privateDefInScope += valOrDef
319322

320323
/** Register pattern variable */
321324
def registerPatVar(patvar: tpd.Bind)(using Context): Unit =
322325
registerUsedAnnotation(patvar.symbol)
323-
patVarsInScope += patvar
326+
if !patvar.symbol.isUnusedAnnot then
327+
patVarsInScope += patvar
324328

325329
/** enter a new scope */
326330
def pushScope(newScopeType: ScopeType): Unit =
@@ -478,6 +482,10 @@ object CheckUnused:
478482
selector.orElse(wildcard) // selector with name or wildcard (or given)
479483
else
480484
None
485+
486+
/** Annotated with @unused */
487+
def isUnusedAnnot(using Context): Boolean =
488+
sym.annotations.exists(a => a.symbol == ctx.definitions.UnusedAnnot)
481489
end UnusedData
482490

483491
private object UnusedData:

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,31 @@ class A {
2929
case x:1 => 0 // error
3030
case x:2 => x // OK
3131
case _ => 1 // OK
32-
}
32+
}
33+
34+
/* ---- CHECK scala.annotation.unused ---- */
35+
package foo.test.scala.annotation:
36+
import annotation.unused // OK
37+
38+
def a1(a: Int) = a // OK
39+
def a2(a: Int) = 1 // error
40+
def a3(@unused a: Int) = 1 //OK
41+
42+
def b1 =
43+
def f = 1 // error
44+
1
45+
46+
def b2 =
47+
def f = 1 // OK
48+
f
49+
50+
def b3 =
51+
@unused def f = 1 // OK
52+
1
53+
54+
object Foo:
55+
private def a = 1 // error
56+
private def b = 2 // OK
57+
@unused private def c = 3 // OK
58+
59+
def other = b

0 commit comments

Comments
 (0)