Skip to content

Commit 0f4be8a

Browse files
committed
Style tweaks in CheckUnused
1 parent e5b76a3 commit 0f4be8a

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

Diff for: compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

+25-28
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import dotty.tools.dotc.core.NameOps.isReplWrapperName
2121
import dotty.tools.dotc.core.Annotations
2222
import dotty.tools.dotc.core.Definitions
2323
import dotty.tools.dotc.core.NameKinds.WildcardParamName
24-
import dotty.tools.dotc.core.Symbols.Symbol
24+
import dotty.tools.dotc.core.Symbols.{Symbol, isDeprecated}
2525
import dotty.tools.dotc.report
2626
import dotty.tools.dotc.reporting.{Message, UnusedSymbol as UnusedSymbolMessage}
2727
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
@@ -76,7 +76,6 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
7676

7777
// ========== MiniPhase Prepare ==========
7878
override def prepareForOther(tree: tpd.Tree)(using Context): Context =
79-
// A standard tree traverser covers cases not handled by the Mega/MiniPhase
8079
traverser.traverse(tree)
8180
ctx
8281

@@ -104,13 +103,13 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
104103
ud.registerUsed(tree.symbol, name, tree.qualifier.tpe, includeForImport = tree.qualifier.span.isSynthetic)
105104

106105
override def prepareForBlock(tree: tpd.Block)(using Context): Context =
107-
pushInBlockTemplatePackageDef(tree)
106+
pushScope(tree)
108107

109108
override def prepareForTemplate(tree: tpd.Template)(using Context): Context =
110-
pushInBlockTemplatePackageDef(tree)
109+
pushScope(tree)
111110

112111
override def prepareForPackageDef(tree: tpd.PackageDef)(using Context): Context =
113-
pushInBlockTemplatePackageDef(tree)
112+
pushScope(tree)
114113

115114
override def prepareForValDef(tree: tpd.ValDef)(using Context): Context =
116115
preparing:
@@ -156,15 +155,15 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
156155
// ========== MiniPhase Transform ==========
157156

158157
override def transformBlock(tree: tpd.Block)(using Context): tpd.Tree =
159-
popOutBlockTemplatePackageDef(tree)
158+
popScope(tree)
160159
tree
161160

162161
override def transformTemplate(tree: tpd.Template)(using Context): tpd.Tree =
163-
popOutBlockTemplatePackageDef(tree)
162+
popScope(tree)
164163
tree
165164

166165
override def transformPackageDef(tree: tpd.PackageDef)(using Context): tpd.Tree =
167-
popOutBlockTemplatePackageDef(tree)
166+
popScope(tree)
168167
tree
169168

170169
override def transformValDef(tree: tpd.ValDef)(using Context): tpd.Tree =
@@ -185,11 +184,11 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
185184

186185
// ---------- MiniPhase HELPERS -----------
187186

188-
private def pushInBlockTemplatePackageDef(tree: tpd.Block | tpd.Template | tpd.PackageDef)(using Context): Context =
187+
private def pushScope(tree: tpd.Block | tpd.Template | tpd.PackageDef)(using Context): Context =
189188
preparing:
190189
ud.pushScope(UnusedData.ScopeType.fromTree(tree))
191190

192-
private def popOutBlockTemplatePackageDef(tree: tpd.Block | tpd.Template | tpd.PackageDef)(using Context): Context =
191+
private def popScope(tree: tpd.Block | tpd.Template | tpd.PackageDef)(using Context): Context =
193192
preparing:
194193
ud.popScope(UnusedData.ScopeType.fromTree(tree))
195194

@@ -198,6 +197,8 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
198197
*
199198
* It traverse the tree the tree and gather the data in the
200199
* corresponding context property
200+
*
201+
* A standard tree traverser covers cases not handled by the Mega/MiniPhase
201202
*/
202203
private def traverser = new TreeTraverser:
203204

@@ -220,9 +221,9 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
220221
traverseChildren(tree)(using newCtx)
221222
case tree: (tpd.Block | tpd.Template | tpd.PackageDef) =>
222223
//! DIFFERS FROM MINIPHASE
223-
pushInBlockTemplatePackageDef(tree)
224+
pushScope(tree)
224225
traverseChildren(tree)(using newCtx)
225-
popOutBlockTemplatePackageDef(tree)
226+
popScope(tree)
226227
case t: tpd.ValDef =>
227228
prepareForValDef(t)
228229
traverseChildren(tree)(using newCtx)
@@ -335,6 +336,7 @@ object CheckUnused:
335336

336337
/** The current scope during the tree traversal */
337338
val currScopeType: Stack[ScopeType] = Stack(ScopeType.Other)
339+
inline def peekScopeType = currScopeType.top
338340

339341
var unusedAggregate: Option[UnusedResult] = None
340342

@@ -427,7 +429,7 @@ object CheckUnused:
427429
!tpd.languageImport(imp.expr).nonEmpty
428430
&& !imp.isGeneratedByEnum
429431
&& !isTransparentAndInline(imp)
430-
&& currScopeType.top != ScopeType.ReplWrapper // #18383 Do not report top-level import's in the repl as unused
432+
&& peekScopeType != ScopeType.ReplWrapper // #18383 Do not report top-level import's in the repl as unused
431433
then
432434
val qualTpe = imp.expr.tpe
433435

@@ -455,7 +457,7 @@ object CheckUnused:
455457
implicitParamInScope += memDef
456458
else if !paramsToSkip.contains(memDef.symbol) then
457459
explicitParamInScope += memDef
458-
else if currScopeType.top == ScopeType.Local then
460+
else if peekScopeType == ScopeType.Local then
459461
localDefInScope += memDef
460462
else if memDef.shouldReportPrivateDef then
461463
privateDefInScope += memDef
@@ -653,14 +655,11 @@ object CheckUnused:
653655
if altName.exists(explicitName => selector.rename != explicitName.toTermName) then
654656
// if there is an explicit name, it must match
655657
false
656-
else
657-
(isDerived || prefix.typeSymbol.isPackageObject || selData.qualTpe =:= prefix) && (
658-
if isDerived then
659-
// See i15503i.scala, grep for "package foo.test.i17156"
660-
selData.allSymbolsDealiasedForNamed.contains(sym.dealiasAsType)
661-
else
662-
selData.allSymbolsForNamed.contains(sym)
663-
)
658+
else if isDerived then
659+
// See i15503i.scala, grep for "package foo.test.i17156"
660+
selData.allSymbolsDealiasedForNamed.contains(sym.dealiasAsType)
661+
else (prefix.typeSymbol.isPackageObject || selData.qualTpe =:= prefix) &&
662+
selData.allSymbolsForNamed.contains(sym)
664663
else
665664
// Wildcard
666665
if !selData.qualTpe.member(sym.name).hasAltWith(_.symbol == sym) then
@@ -687,9 +686,7 @@ object CheckUnused:
687686
val owner = sym.owner
688687
trivialDefs(owner) || // is a trivial def
689688
owner.isPrimaryConstructor ||
690-
owner.annotations.exists ( // @depreacated
691-
_.symbol == ctx.definitions.DeprecatedAnnot
692-
) ||
689+
owner.isDeprecated ||
693690
owner.isAllOf(Synthetic | PrivateLocal) ||
694691
owner.is(Accessor) ||
695692
owner.isOverridden
@@ -743,7 +740,7 @@ object CheckUnused:
743740
!sym.shouldNotReportParamOwner
744741

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

748745
private def isUnsetVarDef(using Context): Boolean =
749746
val sym = memDef.symbol
@@ -770,7 +767,7 @@ object CheckUnused:
770767
object ScopeType:
771768
/** return the scope corresponding to the enclosing scope of the given tree */
772769
def fromTree(tree: tpd.Tree)(using Context): ScopeType = tree match
773-
case tree: tpd.Template => if tree.symbol.name.isReplWrapperName then ReplWrapper else Template
770+
case _: tpd.Template => if tree.symbol.name.isReplWrapperName then ReplWrapper else Template
774771
case _: tpd.Block => Local
775772
case _ => Other
776773

@@ -827,5 +824,5 @@ object CheckUnused:
827824
case tp: NamedType => tp.prefix
828825
case tp: ClassInfo => tp.prefix
829826
case tp: TypeProxy => tp.superType.normalizedPrefix
830-
case _ => tp
827+
case _ => NoType
831828
end CheckUnused

0 commit comments

Comments
 (0)