From 8e3f0c95c3dc5a7e24f42f6de5817d36c5a5815b Mon Sep 17 00:00:00 2001 From: Wojciech Mazur Date: Mon, 8 Jul 2024 19:47:37 +0200 Subject: [PATCH] Add error code to diagnostics about unused code (#19780) Co-authored-by: ghostbuster91 [Cherry-picked 3fdb2923f83acd2ef8910c9f71a394c46be4e268][modified] --- .../tools/dotc/reporting/ErrorMessageID.scala | 5 ++++- .../tools/dotc/reporting/MessageKind.scala | 2 ++ .../dotty/tools/dotc/reporting/messages.scala | 17 +++++++++++++++++ .../tools/dotc/transform/CheckUnused.scala | 17 +++++++++-------- compiler/test-resources/repl/i18383 | 2 +- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala b/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala index 904893911a6d..b832832590d9 100644 --- a/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala +++ b/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala @@ -208,7 +208,10 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe case UnstableInlineAccessorID // errorNumber: 192 - unused in LTS case VolatileOnValID // errorNumber: 193 case ExtensionNullifiedByMemberID // errorNumber: 194 - case InlinedAnonClassWarningID // errorNumber: 195 + case ConstructorProxyNotValueID // errorNumber: 195 - unused in LTS + case ContextBoundCompanionNotValueID // errorNumber: 196 - unused in LTS + case InlinedAnonClassWarningID // errorNumber: 197 + case UnusedSymbolID // errorNumber: 198 def errorNumber = ordinal - 1 diff --git a/compiler/src/dotty/tools/dotc/reporting/MessageKind.scala b/compiler/src/dotty/tools/dotc/reporting/MessageKind.scala index f039ed900a76..10ad4f83d93d 100644 --- a/compiler/src/dotty/tools/dotc/reporting/MessageKind.scala +++ b/compiler/src/dotty/tools/dotc/reporting/MessageKind.scala @@ -21,6 +21,7 @@ enum MessageKind: case MatchCaseUnreachable case Compatibility case PotentialIssue + case UnusedSymbol /** Human readable message that will end up being shown to the user. * NOTE: This is only used in the situation where you have multiple words @@ -37,5 +38,6 @@ enum MessageKind: case PatternMatchExhaustivity => "Pattern Match Exhaustivity" case MatchCaseUnreachable => "Match case Unreachable" case PotentialIssue => "Potential Issue" + case UnusedSymbol => "Unused Symbol" case kind => kind.toString end MessageKind diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index 679c47988199..5874712f83e6 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -3103,3 +3103,20 @@ class VolatileOnVal()(using Context) extends SyntaxMsg(VolatileOnValID): protected def msg(using Context): String = "values cannot be volatile" protected def explain(using Context): String = "" + +class UnusedSymbol(errorText: String)(using Context) +extends Message(UnusedSymbolID) { + def kind = MessageKind.UnusedSymbol + + override def msg(using Context) = errorText + override def explain(using Context) = "" +} + +object UnusedSymbol { + def imports(using Context): UnusedSymbol = new UnusedSymbol(i"unused import") + def localDefs(using Context): UnusedSymbol = new UnusedSymbol(i"unused local definition") + def explicitParams(using Context): UnusedSymbol = new UnusedSymbol(i"unused explicit parameter") + def implicitParams(using Context): UnusedSymbol = new UnusedSymbol(i"unused implicit parameter") + def privateMembers(using Context): UnusedSymbol = new UnusedSymbol(i"unused private member") + def patVars(using Context): UnusedSymbol = new UnusedSymbol(i"unused pattern variable") +} diff --git a/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala b/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala index d420fe78107e..d8389ff964a4 100644 --- a/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala +++ b/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala @@ -17,6 +17,7 @@ import dotty.tools.dotc.core.Phases.Phase import dotty.tools.dotc.core.StdNames import dotty.tools.dotc.report import dotty.tools.dotc.reporting.Message +import dotty.tools.dotc.reporting.UnusedSymbol as UnusedSymbolMessage import dotty.tools.dotc.typer.ImportInfo import dotty.tools.dotc.util.{Property, SrcPos} import dotty.tools.dotc.core.Mode @@ -295,21 +296,21 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke res.warnings.toList.sortBy(_.pos.span.point)(using Ordering[Int]).foreach { s => s match case UnusedSymbol(t, _, WarnTypes.Imports) => - report.warning(s"unused import", t) + report.warning(UnusedSymbolMessage.imports, t) case UnusedSymbol(t, _, WarnTypes.LocalDefs) => - report.warning(s"unused local definition", t) + report.warning(UnusedSymbolMessage.localDefs, t) case UnusedSymbol(t, _, WarnTypes.ExplicitParams) => - report.warning(s"unused explicit parameter", t) + report.warning(UnusedSymbolMessage.explicitParams, t) case UnusedSymbol(t, _, WarnTypes.ImplicitParams) => - report.warning(s"unused implicit parameter", t) + report.warning(UnusedSymbolMessage.implicitParams, t) case UnusedSymbol(t, _, WarnTypes.PrivateMembers) => - report.warning(s"unused private member", t) + report.warning(UnusedSymbolMessage.privateMembers, t) case UnusedSymbol(t, _, WarnTypes.PatVars) => - report.warning(s"unused pattern variable", t) + report.warning(UnusedSymbolMessage.patVars, t) case UnusedSymbol(t, _, WarnTypes.UnsetLocals) => - report.warning(s"unset local variable, consider using an immutable val instead", t) + report.warning("unset local variable, consider using an immutable val instead", t) case UnusedSymbol(t, _, WarnTypes.UnsetPrivates) => - report.warning(s"unset private variable, consider using an immutable val instead", t) + report.warning("unset private variable, consider using an immutable val instead", t) } end CheckUnused diff --git a/compiler/test-resources/repl/i18383 b/compiler/test-resources/repl/i18383 index 81d3c9d5a7fd..563495e2e999 100644 --- a/compiler/test-resources/repl/i18383 +++ b/compiler/test-resources/repl/i18383 @@ -4,7 +4,7 @@ scala> import scala.collection.* scala> class Foo { import scala.util.*; println("foo") } 1 warning found --- Warning: -------------------------------------------------------------------- +-- [E198] Unused Symbol Warning: ----------------------------------------------- 1 | class Foo { import scala.util.*; println("foo") } | ^ | unused import