@@ -52,6 +52,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
5252 tree
5353
5454 override def transformIdent (tree : Ident )(using Context ): tree.type =
55+ refInfos.isAssignment = tree.hasAttachment(AssignmentTarget )
5556 if tree.symbol.exists then
5657 // if in an inline expansion, resolve at summonInline (synthetic pos) or in an enclosing call site
5758 val resolving =
@@ -68,10 +69,12 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
6869 resolveUsage(tree.symbol, tree.name, tree.typeOpt.importPrefix.skipPackageObject)
6970 else if tree.hasType then
7071 resolveUsage(tree.tpe.classSymbol, tree.name, tree.tpe.importPrefix.skipPackageObject)
72+ refInfos.isAssignment = false
7173 tree
7274
7375 // import x.y; y may be rewritten x.y, also import x.z as y
7476 override def transformSelect (tree : Select )(using Context ): tree.type =
77+ refInfos.isAssignment = tree.hasAttachment(AssignmentTarget )
7578 val name = tree.removeAttachment(OriginalName ).getOrElse(nme.NO_NAME )
7679 inline def isImportable = tree.qualifier.srcPos.isSynthetic
7780 && tree.qualifier.tpe.match
@@ -92,6 +95,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
9295 resolveUsage(tree.symbol, name, tree.qualifier.tpe)
9396 else if ! ignoreTree(tree) then
9497 refUsage(tree.symbol)
98+ refInfos.isAssignment = false
9599 tree
96100
97101 override def transformLiteral (tree : Literal )(using Context ): tree.type =
@@ -113,13 +117,10 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
113117 ctx
114118
115119 override def prepareForAssign (tree : Assign )(using Context ): Context =
116- tree.lhs.putAttachment(Ignore , ()) // don't take LHS reference as a read
120+ tree.lhs.putAttachment(AssignmentTarget , ()) // don't take LHS reference as a read
117121 ctx
118122 override def transformAssign (tree : Assign )(using Context ): tree.type =
119- tree.lhs.removeAttachment(Ignore )
120- val sym = tree.lhs.symbol
121- if sym.exists then
122- refInfos.asss.addOne(sym)
123+ tree.lhs.removeAttachment(AssignmentTarget )
123124 tree
124125
125126 override def prepareForMatch (tree : Match )(using Context ): Context =
@@ -269,7 +270,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
269270 // if sym is not an enclosing element, record the reference
270271 def refUsage (sym : Symbol )(using Context ): Unit =
271272 if ! ctx.outersIterator.exists(cur => cur.owner eq sym) then
272- refInfos.refs.addOne (sym)
273+ refInfos.addRef (sym)
273274
274275 /** Look up a reference in enclosing contexts to determine whether it was introduced by a definition or import.
275276 * The binding of highest precedence must then be correct.
@@ -328,7 +329,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
328329 case none =>
329330
330331 // Avoid spurious NoSymbol and also primary ctors which are never warned about.
331- // Selections C.this.toString should be already excluded, but backtopped here for eq, etc.
332+ // Selections C.this.toString should be already excluded, but backstopped here for eq, etc.
332333 if ! sym.exists || sym.isPrimaryConstructor || sym.isEffectiveRoot || defn.topClasses(sym.owner) then return
333334
334335 // Find the innermost, highest precedence. Contexts have no nesting levels but assume correctness.
@@ -398,7 +399,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
398399 end while
399400 // record usage and possibly an import
400401 if ! enclosed then
401- refInfos.refs.addOne (sym)
402+ refInfos.addRef (sym)
402403 if candidate != NoContext && candidate.isImportContext && importer != null then
403404 refInfos.sels.put(importer, ())
404405 // possibly record that we have performed this look-up
@@ -437,6 +438,9 @@ object CheckUnused:
437438 /** Ignore reference. */
438439 val Ignore = Property .StickyKey [Unit ]
439440
441+ /** Tree is LHS of Assign. */
442+ val AssignmentTarget = Property .StickyKey [Unit ]
443+
440444 class PostTyper extends CheckUnused (PhaseMode .Aggregate , " PostTyper" )
441445
442446 class PostInlining extends CheckUnused (PhaseMode .Report , " PostInlining" )
@@ -481,6 +485,14 @@ object CheckUnused:
481485
482486 val inlined = Stack .empty[SrcPos ] // enclosing call.srcPos of inlined code (expansions)
483487 var inliners = 0 // depth of inline def (not inlined yet)
488+
489+ // instead of refs.addOne, use addRef to distinguish a read from a write to var
490+ var isAssignment = false
491+ def addRef (sym : Symbol ): Unit =
492+ if isAssignment then
493+ asss.addOne(sym)
494+ else
495+ refs.addOne(sym)
484496 end RefInfos
485497
486498 // Symbols already resolved in the given Context (with name and prefix of lookup).
0 commit comments