Skip to content

Commit 3fdb292

Browse files
Add error code to diagnostics about unused code (#19780)
Co-authored-by: ghostbuster91 <[email protected]>
1 parent 9b5ab2e commit 3fdb292

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
211211
case ConstructorProxyNotValueID // errorNumber: 195
212212
case ContextBoundCompanionNotValueID // errorNumber: 196
213213
case InlinedAnonClassWarningID // errorNumber: 197
214+
case UnusedSymbolID // errorNumber: 198
214215

215216
def errorNumber = ordinal - 1
216217

compiler/src/dotty/tools/dotc/reporting/MessageKind.scala

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ enum MessageKind:
2121
case MatchCaseUnreachable
2222
case Compatibility
2323
case PotentialIssue
24+
case UnusedSymbol
2425

2526
/** Human readable message that will end up being shown to the user.
2627
* NOTE: This is only used in the situation where you have multiple words
@@ -37,5 +38,6 @@ enum MessageKind:
3738
case PatternMatchExhaustivity => "Pattern Match Exhaustivity"
3839
case MatchCaseUnreachable => "Match case Unreachable"
3940
case PotentialIssue => "Potential Issue"
41+
case UnusedSymbol => "Unused Symbol"
4042
case kind => kind.toString
4143
end MessageKind

compiler/src/dotty/tools/dotc/reporting/messages.scala

+16
Original file line numberDiff line numberDiff line change
@@ -3239,3 +3239,19 @@ extends TypeMsg(ConstructorProxyNotValueID):
32393239
|companion value with the (term-)name `A`. However, these context bound companions
32403240
|are not values themselves, they can only be referred to in selections."""
32413241

3242+
class UnusedSymbol(errorText: String)(using Context)
3243+
extends Message(UnusedSymbolID) {
3244+
def kind = MessageKind.UnusedSymbol
3245+
3246+
override def msg(using Context) = errorText
3247+
override def explain(using Context) = ""
3248+
}
3249+
3250+
object UnusedSymbol {
3251+
def imports(using Context): UnusedSymbol = new UnusedSymbol(i"unused import")
3252+
def localDefs(using Context): UnusedSymbol = new UnusedSymbol(i"unused local definition")
3253+
def explicitParams(using Context): UnusedSymbol = new UnusedSymbol(i"unused explicit parameter")
3254+
def implicitParams(using Context): UnusedSymbol = new UnusedSymbol(i"unused implicit parameter")
3255+
def privateMembers(using Context): UnusedSymbol = new UnusedSymbol(i"unused private member")
3256+
def patVars(using Context): UnusedSymbol = new UnusedSymbol(i"unused pattern variable")
3257+
}

compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

+9-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import dotty.tools.dotc.core.Phases.Phase
1717
import dotty.tools.dotc.core.StdNames
1818
import dotty.tools.dotc.report
1919
import dotty.tools.dotc.reporting.Message
20+
import dotty.tools.dotc.reporting.UnusedSymbol as UnusedSymbolMessage
2021
import dotty.tools.dotc.typer.ImportInfo
2122
import dotty.tools.dotc.util.{Property, SrcPos}
2223
import dotty.tools.dotc.core.Mode
@@ -295,21 +296,21 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
295296
res.warnings.toList.sortBy(_.pos.span.point)(using Ordering[Int]).foreach { s =>
296297
s match
297298
case UnusedSymbol(t, _, WarnTypes.Imports) =>
298-
report.warning(s"unused import", t)
299+
report.warning(UnusedSymbolMessage.imports, t)
299300
case UnusedSymbol(t, _, WarnTypes.LocalDefs) =>
300-
report.warning(s"unused local definition", t)
301+
report.warning(UnusedSymbolMessage.localDefs, t)
301302
case UnusedSymbol(t, _, WarnTypes.ExplicitParams) =>
302-
report.warning(s"unused explicit parameter", t)
303+
report.warning(UnusedSymbolMessage.explicitParams, t)
303304
case UnusedSymbol(t, _, WarnTypes.ImplicitParams) =>
304-
report.warning(s"unused implicit parameter", t)
305+
report.warning(UnusedSymbolMessage.implicitParams, t)
305306
case UnusedSymbol(t, _, WarnTypes.PrivateMembers) =>
306-
report.warning(s"unused private member", t)
307+
report.warning(UnusedSymbolMessage.privateMembers, t)
307308
case UnusedSymbol(t, _, WarnTypes.PatVars) =>
308-
report.warning(s"unused pattern variable", t)
309+
report.warning(UnusedSymbolMessage.patVars, t)
309310
case UnusedSymbol(t, _, WarnTypes.UnsetLocals) =>
310-
report.warning(s"unset local variable, consider using an immutable val instead", t)
311+
report.warning("unset local variable, consider using an immutable val instead", t)
311312
case UnusedSymbol(t, _, WarnTypes.UnsetPrivates) =>
312-
report.warning(s"unset private variable, consider using an immutable val instead", t)
313+
report.warning("unset private variable, consider using an immutable val instead", t)
313314
}
314315

315316
end CheckUnused

compiler/test-resources/repl/i18383

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ scala> import scala.collection.*
44

55
scala> class Foo { import scala.util.*; println("foo") }
66
1 warning found
7-
-- Warning: --------------------------------------------------------------------
7+
-- [E198] Unused Symbol Warning: -----------------------------------------------
88
1 | class Foo { import scala.util.*; println("foo") }
99
| ^
1010
| unused import

0 commit comments

Comments
 (0)