diff --git a/compiler/src/dotty/tools/dotc/reporting/Diagnostic.scala b/compiler/src/dotty/tools/dotc/reporting/Diagnostic.scala index 30a9b8f60cf8..da938f761fbc 100644 --- a/compiler/src/dotty/tools/dotc/reporting/Diagnostic.scala +++ b/compiler/src/dotty/tools/dotc/reporting/Diagnostic.scala @@ -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( diff --git a/compiler/src/dotty/tools/dotc/reporting/Reporter.scala b/compiler/src/dotty/tools/dotc/reporting/Reporter.scala index af92d0e0efdf..6eeac9832197 100644 --- a/compiler/src/dotty/tools/dotc/reporting/Reporter.scala +++ b/compiler/src/dotty/tools/dotc/reporting/Reporter.scala @@ -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} @@ -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 => diff --git a/compiler/src/dotty/tools/dotc/reporting/WConf.scala b/compiler/src/dotty/tools/dotc/reporting/WConf.scala index cff15aa6dc38..2cb170ec1764 100644 --- a/compiler/src/dotty/tools/dotc/reporting/WConf.scala +++ b/compiler/src/dotty/tools/dotc/reporting/WConf.scala @@ -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 diff --git a/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala b/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala index 1c6bae6b112a..d935e1563bf0 100644 --- a/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala +++ b/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala @@ -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 @@ -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 @@ -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 = diff --git a/tests/rewrites/i24009.scala b/tests/rewrites/i24009.scala new file mode 100644 index 000000000000..73620d558e51 --- /dev/null +++ b/tests/rewrites/i24009.scala @@ -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