Skip to content

Commit 75a5fe7

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 135b7ca commit 75a5fe7

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}
@@ -174,6 +175,8 @@ abstract class Reporter extends interfaces.ReporterResult {
174175
handleRecursive("error reporting", dia.message, ex)
175176
dia match {
176177
case w: Warning =>
178+
if w.isInstanceOf[LintWarning] then
179+
w.msg.actions.foreach(Rewrites.applyAction)
177180
warnings = w :: warnings
178181
_warningCount += 1
179182
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
@@ -11,8 +11,8 @@ import Scopes.newScope
1111
import StdNames.nme
1212
import Symbols.{ClassSymbol, NoSymbol, Symbol, defn, isDeprecated, requiredClass, requiredModule}
1313
import Types.*
14-
import reporting.{Action, CodeAction, Diagnostic, UnusedSymbol, WConf}
15-
import rewrites.Rewrites
14+
import reporting.{CodeAction, Diagnostic, UnusedSymbol}
15+
import rewrites.Rewrites.ActionPatch
1616

1717
import MegaPhase.MiniPhase
1818
import typer.{ImportInfo, Typer}
@@ -555,29 +555,15 @@ object CheckUnused:
555555

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

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

577562
def warnings(using Context): Array[MessageInfo] =
578563
val actionable = ctx.settings.rewrite.value.nonEmpty
579564
val warnings = ArrayBuilder.make[MessageInfo]
580-
def warnAt(pos: SrcPos)(msg: UnusedSymbol, origin: String = ""): Unit = warnings.addOne((msg, pos, origin))
565+
def warnAt(pos: SrcPos)(msg: UnusedSymbol, origin: String = Diagnostic.OriginWarning.NoOrigin): Unit =
566+
warnings.addOne((msg, pos, origin))
581567
val infos = refInfos
582568

583569
// non-local sym was target of assignment or has a sibling setter that was referenced
@@ -740,7 +726,6 @@ object CheckUnused:
740726

741727
def checkImports() =
742728
import scala.jdk.CollectionConverters.given
743-
import Rewrites.ActionPatch
744729
type ImpSel = (Import, ImportSelector)
745730
def isUsed(sel: ImportSelector): Boolean = infos.sels.containsKey(sel)
746731
def warnImport(warnable: ImpSel, actions: List[CodeAction] = Nil): Unit =

0 commit comments

Comments
 (0)