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 d2737c0cc559..ccb236b4173f 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -3117,3 +3117,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 diff --git a/tests/warn/i16723.check b/tests/warn/i16723.check index ed8e55502a80..6d55fa0a89d2 100644 --- a/tests/warn/i16723.check +++ b/tests/warn/i16723.check @@ -1,4 +1,4 @@ --- [E195] Potential Issue Warning: tests/warn/i16723.scala:3:2 --------------------------------------------------------- +-- [E197] Potential Issue Warning: tests/warn/i16723.scala:3:2 --------------------------------------------------------- 3 | new Object {} // warn | ^ | New anonymous class definition will be duplicated at each inline site diff --git a/tests/warn/i16723a.check b/tests/warn/i16723a.check index ba4794fac23e..ace11c5af1f9 100644 --- a/tests/warn/i16723a.check +++ b/tests/warn/i16723a.check @@ -1,4 +1,4 @@ --- [E195] Potential Issue Warning: tests/warn/i16723a.scala:5:38 ------------------------------------------------------- +-- [E197] Potential Issue Warning: tests/warn/i16723a.scala:5:38 ------------------------------------------------------- 5 |inline given Converter[Int, String] = new Converter { // warn | ^ | New anonymous class definition will be duplicated at each inline site