Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/Diagnostic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ object Diagnostic:
*/
trait OriginWarning(val origin: String):
self: Warning =>
object OriginWarning:
val NoOrigin = "..."

/** Lints are likely to be filtered. Provide extra axes for filtering by `-Wconf`.
*/
class LintWarning(msg: Message, pos: SourcePosition, origin: String)
class LintWarning(msg: Message, pos: SourcePosition, origin: String = OriginWarning.NoOrigin)
extends Warning(msg, pos), OriginWarning(origin)

class Warning(
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import dotty.tools.dotc.core.Mode
import dotty.tools.dotc.core.Symbols.{NoSymbol, Symbol}
import dotty.tools.dotc.reporting.Diagnostic.*
import dotty.tools.dotc.reporting.Message.*
import dotty.tools.dotc.rewrites.Rewrites
import dotty.tools.dotc.util.NoSourcePosition

import java.io.{BufferedReader, PrintWriter}
Expand Down Expand Up @@ -170,6 +171,8 @@ abstract class Reporter extends interfaces.ReporterResult {
handleRecursive("error reporting", dia.message, ex)
dia match {
case w: Warning =>
if w.isInstanceOf[LintWarning] then
w.msg.actions.foreach(Rewrites.applyAction)
warnings = w :: warnings
_warningCount += 1
case e: Error =>
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/WConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ enum MessageFilter:
pattern.findFirstIn(path).nonEmpty
case Origin(pattern) =>
message match
case message: OriginWarning => pattern.findFirstIn(message.origin).nonEmpty
case message: OriginWarning if message.origin != OriginWarning.NoOrigin =>
pattern.findFirstIn(message.origin).nonEmpty
case _ => false
case None => false

Expand Down
14 changes: 6 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import dotty.tools.dotc.core.StdNames.nme
import dotty.tools.dotc.core.Symbols.{ClassSymbol, NoSymbol, Symbol, defn, isDeprecated, requiredClass, requiredModule}
import dotty.tools.dotc.core.Types.*
import dotty.tools.dotc.report
import dotty.tools.dotc.reporting.{CodeAction, UnusedSymbol}
import dotty.tools.dotc.rewrites.Rewrites
import dotty.tools.dotc.reporting.{CodeAction, Diagnostic, UnusedSymbol}
import dotty.tools.dotc.rewrites.Rewrites.ActionPatch
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
import dotty.tools.dotc.typer.{ImportInfo, Typer}
import dotty.tools.dotc.typer.Deriving.OriginalTypeClass
Expand Down Expand Up @@ -549,16 +549,15 @@ object CheckUnused:

def reportUnused()(using Context): Unit =
for (msg, pos, origin) <- warnings do
if origin.isEmpty then report.warning(msg, pos)
else report.warning(msg, pos, origin)
msg.actions.headOption.foreach(Rewrites.applyAction)
report.warning(msg, pos, origin)

type MessageInfo = (UnusedSymbol, SrcPos, String) // string is origin or empty

def warnings(using Context): Array[MessageInfo] =
val actionable = ctx.settings.rewrite.value.nonEmpty
val actionable: true = true //ctx.settings.rewrite.value.nonEmpty
val warnings = ArrayBuilder.make[MessageInfo]
def warnAt(pos: SrcPos)(msg: UnusedSymbol, origin: String = ""): Unit = warnings.addOne((msg, pos, origin))
def warnAt(pos: SrcPos)(msg: UnusedSymbol, origin: String = Diagnostic.OriginWarning.NoOrigin): Unit =
warnings.addOne((msg, pos, origin))
val infos = refInfos

// non-local sym was target of assignment or has a sibling setter that was referenced
Expand Down Expand Up @@ -721,7 +720,6 @@ object CheckUnused:

def checkImports() =
import scala.jdk.CollectionConverters.given
import Rewrites.ActionPatch
type ImpSel = (Import, ImportSelector)
def isUsed(sel: ImportSelector): Boolean = infos.sels.containsKey(sel)
def warnImport(warnable: ImpSel, actions: List[CodeAction] = Nil): Unit =
Expand Down
9 changes: 9 additions & 0 deletions tests/rewrites/i24009.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//> using options -Wunused:imports -rewrite -Wconf:name=UnusedSymbol&origin=p.C:s

package p:
class C

package q:
import p.C // nowarn and no rewrite

class D
Loading