Skip to content

Commit ff6bff7

Browse files
authored
Merge pull request #636 from scala/backport-lts-3.3-24055
Backport "Unused lint does not rewrite suppressed warnings" to 3.3 LTS
2 parents 23a6b76 + fd8574b commit ff6bff7

File tree

10 files changed

+40
-25
lines changed

10 files changed

+40
-25
lines changed

compiler/src/dotty/tools/dotc/Driver.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ class Driver {
9292
case "help" =>
9393
case reporterClassName =>
9494
try
95-
Class.forName(reporterClassName).getDeclaredConstructor().newInstance() match
96-
case userReporter: Reporter =>
97-
ictx.setReporter(userReporter)
98-
case badReporter => report.error:
99-
em"Not a reporter: ${ctx.settings.Yreporter.value}"
95+
val newCls = Class.forName(reporterClassName).nn
96+
val newInst = newCls.getDeclaredConstructor().nn.newInstance().nn
97+
newInst match
98+
case userReporter: Reporter =>
99+
ictx.setReporter(userReporter)
100+
case badReporter => report.error:
101+
em"Not a reporter: ${ctx.settings.Yreporter.value}"
100102
catch case e: ReflectiveOperationException => report.error:
101103
em"Could not create reporter ${ctx.settings.Yreporter.value}: ${e}"
102104
}

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ private sealed trait YSettings:
377377
val YdisableFlatCpCaching: Setting[Boolean] = BooleanSetting("-YdisableFlatCpCaching", "Do not cache flat classpath representation of classpath elements from jars across compiler instances.")
378378

379379
val Yscala2Unpickler: Setting[String] = StringSetting("-Yscala2-unpickler", "", "Control where we may get Scala 2 symbols from. This is either \"always\", \"never\", or a classpath.", "always")
380-
val YnoReporter: Setting[Boolean] = BooleanSetting("-Yno-reporter", "Diagnostics are silently consumed")
381380
val Yreporter: Setting[String] = StringSetting(name = "-Yreporter", helpArg = "<class>", descr = "Specify a dotty.tools.dotc.reporting.Reporter", default = "dotty.tools.dotc.reporting.ConsoleReporter")
382381

383382
val YnoImports: Setting[Boolean] = BooleanSetting("-Yno-imports", "Compile without importing scala.*, java.lang.*, or Predef.")

compiler/src/dotty/tools/dotc/interactive/Completion.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,8 @@ object Completion:
576576
termRef.denot.asSingleDenotation.mapInfo(_ => tpe)
577577
}
578578
catch case NonFatal(ex) =>
579-
logger.warning(
580-
s"Exception when trying to apply extension method:\n ${ex.getMessage()}\n${ex.getStackTrace().mkString("\n")}"
579+
logger.nn.warning(
580+
s"Exception when trying to apply extension method:\n ${ex.getMessage()}\n${ex.getStackTrace().nn.mkString("\n")}"
581581
)
582582
None
583583

@@ -684,8 +684,8 @@ object Completion:
684684
interactiv.println(i"implicit conversion targets considered: ${conversions.toList}%, %")
685685
conversions
686686
} catch case NonFatal(ex) =>
687-
logger.warning(
688-
s"Exception when searching for implicit conversions:\n ${ex.getMessage()}\n${ex.getStackTrace().mkString("\n")}"
687+
logger.nn.warning(
688+
s"Exception when searching for implicit conversions:\n ${ex.getMessage()}\n${ex.getStackTrace().nn.mkString("\n")}"
689689
)
690690
Set.empty
691691

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ object Diagnostic:
4242
*/
4343
trait OriginWarning(val origin: String):
4444
self: Warning =>
45+
object OriginWarning:
46+
val NoOrigin = "..."
4547

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

5153
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: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import dotty.tools.dotc.core.StdNames.nme
1616
import dotty.tools.dotc.core.Symbols.{ClassSymbol, NoSymbol, Symbol, defn, isDeprecated, requiredClass, requiredModule}
1717
import dotty.tools.dotc.core.Types.*
1818
import dotty.tools.dotc.report
19-
import dotty.tools.dotc.reporting.{CodeAction, UnusedSymbol}
20-
import dotty.tools.dotc.rewrites.Rewrites
19+
import dotty.tools.dotc.reporting.{CodeAction, Diagnostic, UnusedSymbol}
20+
import dotty.tools.dotc.rewrites.Rewrites.ActionPatch
2121
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
2222
import dotty.tools.dotc.typer.{ImportInfo, Typer}
2323
import dotty.tools.dotc.typer.Deriving.OriginalTypeClass
@@ -527,16 +527,15 @@ object CheckUnused:
527527

528528
def reportUnused()(using Context): Unit = if !refInfos.isNullified then
529529
for (msg, pos, origin) <- warnings do
530-
if origin.isEmpty then report.warning(msg, pos)
531-
else report.warning(msg, pos, origin)
532-
msg.actions.headOption.foreach(Rewrites.applyAction)
530+
report.warning(msg, pos, origin)
533531

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

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

542541
// non-local sym was target of assignment or has a sibling setter that was referenced
@@ -699,7 +698,6 @@ object CheckUnused:
699698

700699
def checkImports() =
701700
import scala.jdk.CollectionConverters.given
702-
import Rewrites.ActionPatch
703701
type ImpSel = (Import, ImportSelector)
704702
def isUsed(sel: ImportSelector): Boolean = infos.sels.containsKey(sel)
705703
def warnImport(warnable: ImpSel, actions: List[CodeAction] = Nil): Unit =

compiler/src/dotty/tools/dotc/util/Chars.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ object Chars:
137137

138138
def escapedChar(ch: Char): String =
139139
if requiresFormat(ch) then
140-
val b = StringBuilder().append('\'')
140+
val b = StringBuilder().append('\'').nn
141141
escapedChar(b, ch)
142142
b.append('\'').toString
143143
else

presentation-compiler/test/dotty/tools/pc/tests/DiagnosticProviderSuite.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ class DiagnosticProviderSuite extends PcAssertions {
3232
additionalChecks: List[Diagnostic] => Unit = identity
3333
): Unit =
3434
val diagnostics = pc
35-
.didChange(TestVirtualFileParams(URI.create("file:/Diagnostic.scala"), text))
35+
.didChange(TestVirtualFileParams(URI.create("file:/Diagnostic.scala").nn, text))
36+
.nn
3637
.asScala
3738

38-
val actual = diagnostics.map(d => TestDiagnostic(d.getRange().getStart().getOffset(text), d.getRange().getEnd().getOffset(text), d.getMessage(), d.getSeverity()))
39+
val actual = diagnostics.map(d => TestDiagnostic(d.getRange().nn.getStart().nn.getOffset(text), d.getRange().nn.getEnd().nn.getOffset(text), d.getMessage().nn, d.getSeverity().nn))
3940
assertEquals(expected, actual, s"Expected [${expected.mkString(", ")}] but got [${actual.mkString(", ")}]")
4041
additionalChecks(diagnostics.toList)
4142

@@ -77,8 +78,8 @@ class DiagnosticProviderSuite extends PcAssertions {
7778
),
7879
diags =>
7980
val action = diags.head.getData().asInstanceOf[java.util.List[CodeAction]].asScala.head
80-
assertWithDiff("Remove repeated modifier: \"private\"", action.getTitle(), false)
81-
assertEquals(1, action.getEdit().getChanges().size(), "There should be one change")
81+
assertWithDiff("Remove repeated modifier: \"private\"", action.getTitle().nn, false)
82+
assertEquals(1, action.getEdit().nn.getChanges().nn.size(), "There should be one change")
8283
)
8384

8485
}

tests/rewrites/i24009.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//> using options -Wunused:imports -rewrite -Wconf:name=UnusedSymbol&origin=p.C:s
2+
3+
package p:
4+
class C
5+
6+
package q:
7+
import p.C // nowarn and no rewrite
8+
9+
class D

0 commit comments

Comments
 (0)