From 3fdb2923f83acd2ef8910c9f71a394c46be4e268 Mon Sep 17 00:00:00 2001 From: Kasper Kondzielski Date: Tue, 28 May 2024 16:54:15 +0200 Subject: [PATCH] Add error code to diagnostics about unused code (#19780) Co-authored-by: ghostbuster91 --- .../tools/dotc/reporting/ErrorMessageID.scala | 1 + .../tools/dotc/reporting/MessageKind.scala | 2 ++ .../dotty/tools/dotc/reporting/messages.scala | 16 ++++++++++++++++ .../tools/dotc/transform/CheckUnused.scala | 17 +++++++++-------- compiler/test-resources/repl/i18383 | 2 +- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala b/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala index 04380a7b8e4a..0e42629773cc 100644 --- a/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala +++ b/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala @@ -211,6 +211,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe case ConstructorProxyNotValueID // errorNumber: 195 case ContextBoundCompanionNotValueID // errorNumber: 196 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 ceb8ecbc8e03..65f3a478fcd4 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -3239,3 +3239,19 @@ extends TypeMsg(ConstructorProxyNotValueID): |companion value with the (term-)name `A`. However, these context bound companions |are not values themselves, they can only be referred to in selections.""" +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