Skip to content

Commit

Permalink
Backport "Add error code to diagnostics about unused code" to LTS (#2…
Browse files Browse the repository at this point in the history
…1127)

Backports #19780 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jul 9, 2024
2 parents 737c2bc + 96b3a96 commit 9d44884
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 12 deletions.
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/MessageKind.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
17 changes: 17 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
17 changes: 9 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/i18383
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/warn/i16723.check
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/warn/i16723a.check
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 9d44884

Please sign in to comment.