Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error code to diagnostics about unused code #19780

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

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
16 changes: 16 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
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
Loading