Skip to content

Commit c2d9e92

Browse files
committed
Reporter applies lint actions
Instead of testing whether to emit actions by probing suppressions, just let the reporter apply LintWarning actions when the warning is actually reported.
1 parent 6ee08b8 commit c2d9e92

File tree

4 files changed

+13
-22
lines changed

4 files changed

+13
-22
lines changed

compiler/src/dotty/tools/dotc/reporting/Diagnostic.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ object Diagnostic:
4040
*/
4141
trait OriginWarning(val origin: String):
4242
self: Warning =>
43+
object OriginWarning:
44+
val NoOrigin = "..."
4345

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

4951
class Warning(

compiler/src/dotty/tools/dotc/reporting/Reporter.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import dotty.tools.dotc.core.Mode
99
import dotty.tools.dotc.core.Symbols.{NoSymbol, Symbol}
1010
import dotty.tools.dotc.reporting.Diagnostic.*
1111
import dotty.tools.dotc.reporting.Message.*
12+
import dotty.tools.dotc.rewrites.Rewrites
1213
import dotty.tools.dotc.util.NoSourcePosition
1314

1415
import java.io.{BufferedReader, PrintWriter}
@@ -170,6 +171,8 @@ abstract class Reporter extends interfaces.ReporterResult {
170171
handleRecursive("error reporting", dia.message, ex)
171172
dia match {
172173
case w: Warning =>
174+
if w.isInstanceOf[LintWarning] then
175+
w.msg.actions.foreach(Rewrites.applyAction)
173176
warnings = w :: warnings
174177
_warningCount += 1
175178
case e: Error =>

compiler/src/dotty/tools/dotc/reporting/WConf.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ enum MessageFilter:
3434
pattern.findFirstIn(path).nonEmpty
3535
case Origin(pattern) =>
3636
message match
37-
case message: OriginWarning => pattern.findFirstIn(message.origin).nonEmpty
37+
case message: OriginWarning if message.origin != OriginWarning.NoOrigin =>
38+
pattern.findFirstIn(message.origin).nonEmpty
3839
case _ => false
3940
case None => false
4041

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

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import dotty.tools.dotc.core.StdNames.nme
1515
import dotty.tools.dotc.core.Symbols.{ClassSymbol, NoSymbol, Symbol, defn, isDeprecated, requiredClass, requiredModule}
1616
import dotty.tools.dotc.core.Types.*
1717
import dotty.tools.dotc.report
18-
import dotty.tools.dotc.reporting.{Action, CodeAction, Diagnostic, UnusedSymbol, WConf}
19-
import dotty.tools.dotc.rewrites.Rewrites
18+
import dotty.tools.dotc.reporting.{CodeAction, Diagnostic, UnusedSymbol}
19+
import dotty.tools.dotc.rewrites.Rewrites.ActionPatch
2020
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
2121
import dotty.tools.dotc.typer.{ImportInfo, Typer}
2222
import dotty.tools.dotc.typer.Deriving.OriginalTypeClass
@@ -557,29 +557,15 @@ object CheckUnused:
557557

558558
def reportUnused()(using Context): Unit =
559559
for (msg, pos, origin) <- warnings do
560-
if origin.isEmpty then report.warning(msg, pos)
561-
else report.warning(msg, pos, origin)
562-
// avoid rewrite if warning will be suppressed (would be nice if reporter knew how to apply actions)
563-
msg.actions.headOption match
564-
case Some(action) if ctx.run != null =>
565-
val dia =
566-
if origin.isEmpty then Diagnostic.Warning(msg, pos.sourcePos)
567-
else Diagnostic.LintWarning(msg, pos.sourcePos, origin)
568-
ctx.run.nn.suppressions.nowarnAction(dia) match
569-
case Action.Warning =>
570-
WConf.parsed.action(dia) match
571-
case Action.Error | Action.Warning =>
572-
Rewrites.applyAction(action)
573-
case _ =>
574-
case _ =>
575-
case _ =>
560+
report.warning(msg, pos, origin)
576561

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

579564
def warnings(using Context): Array[MessageInfo] =
580565
val actionable = ctx.settings.rewrite.value.nonEmpty
581566
val warnings = ArrayBuilder.make[MessageInfo]
582-
def warnAt(pos: SrcPos)(msg: UnusedSymbol, origin: String = ""): Unit = warnings.addOne((msg, pos, origin))
567+
def warnAt(pos: SrcPos)(msg: UnusedSymbol, origin: String = Diagnostic.OriginWarning.NoOrigin): Unit =
568+
warnings.addOne((msg, pos, origin))
583569
val infos = refInfos
584570

585571
// non-local sym was target of assignment or has a sibling setter that was referenced
@@ -742,7 +728,6 @@ object CheckUnused:
742728

743729
def checkImports() =
744730
import scala.jdk.CollectionConverters.given
745-
import Rewrites.ActionPatch
746731
type ImpSel = (Import, ImportSelector)
747732
def isUsed(sel: ImportSelector): Boolean = infos.sels.containsKey(sel)
748733
def warnImport(warnable: ImpSel, actions: List[CodeAction] = Nil): Unit =

0 commit comments

Comments
 (0)