Skip to content

Commit 8a91af1

Browse files
committed
Add an helpful message for -W option
1 parent 95c8a40 commit 8a91af1

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

compiler/src/dotty/tools/dotc/config/CliCommand.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ trait CliCommand:
6060
def defaultValue = s.default match
6161
case _: Int | _: String => s.default.toString
6262
case _ => ""
63-
val info = List(shortHelp(s), if defaultValue.nonEmpty then s"Default $defaultValue" else "", if s.legalChoices.nonEmpty then s"Choices ${s.legalChoices}" else "")
63+
val info = List(shortHelp(s), if defaultValue.nonEmpty then s"Default $defaultValue" else "", if s.legalChoices.nonEmpty then s"Choices : ${s.legalChoices}" else "")
6464
(s.name, info.filter(_.nonEmpty).mkString("\n"))
6565
end help
6666

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,27 @@ private sealed trait VerboseSettings:
156156
*/
157157
private sealed trait WarningSettings:
158158
self: SettingGroup =>
159+
import Setting.ChoiceWithHelp
160+
159161
val Whelp: Setting[Boolean] = BooleanSetting("-W", "Print a synopsis of warning options.")
160162
val XfatalWarnings: Setting[Boolean] = BooleanSetting("-Werror", "Fail the compilation if there are any warnings.", aliases = List("-Xfatal-warnings"))
161163

162-
val Wunused: Setting[List[String]] = MultiChoiceSetting(
164+
val Wunused: Setting[List[ChoiceWithHelp[String]]] = MultiChoiceHelpSetting(
163165
name = "-Wunused",
164166
helpArg = "warning",
165167
descr = "Enable or disable specific `unused` warnings",
166-
choices = List("nowarn", "all", "imports", "locals", "privates", "patvars", "explicits", "implicits", "params", "linted"),
168+
choices = List(
169+
ChoiceWithHelp("nowarn", ""),
170+
ChoiceWithHelp("all",""),
171+
ChoiceWithHelp("imports","Warn if an import selector is not referenced."),
172+
ChoiceWithHelp("patvars","Warn if a variable bound in a pattern is unused."),
173+
ChoiceWithHelp("privates","Warn if a private member is unused."),
174+
ChoiceWithHelp("locals","Warn if a local definition is unused."),
175+
ChoiceWithHelp("explicits","Warn if an explicit parameter is unused."),
176+
ChoiceWithHelp("implicits","Warn if an implicit parameter is unused."),
177+
ChoiceWithHelp("params","Enable -Wunused:explicits,implicits."),
178+
ChoiceWithHelp("linted","Enable -Wunused:imports,privates,locals,implicits.")
179+
),
167180
default = Nil
168181
)
169182
object WunusedHas:

compiler/src/dotty/tools/dotc/config/Settings.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import annotation.tailrec
1111
import collection.mutable.ArrayBuffer
1212
import reflect.ClassTag
1313
import scala.util.{Success, Failure}
14+
import dotty.tools.dotc.config.Settings.Setting.ChoiceWithHelp
1415

1516
object Settings:
1617

@@ -184,6 +185,19 @@ object Settings:
184185
def update(x: T)(using Context): SettingsState = setting.updateIn(ctx.settingsState, x)
185186
def isDefault(using Context): Boolean = setting.isDefaultIn(ctx.settingsState)
186187

188+
/**
189+
* A choice with help description.
190+
*
191+
* NOTE : `equals` and `toString` have special behaviors
192+
*/
193+
case class ChoiceWithHelp[T](choice: T, description: String):
194+
override def equals(x: Any): Boolean = x match
195+
case s:String => s == choice.toString()
196+
case _ => false
197+
override def toString(): String =
198+
s"\n\t- $choice${if description.isBlank() then "" else s"\t: $description"}"
199+
end Setting
200+
187201
class SettingGroup {
188202

189203
private val _allSettings = new ArrayBuffer[Setting[?]]
@@ -265,6 +279,9 @@ object Settings:
265279
def MultiChoiceSetting(name: String, helpArg: String, descr: String, choices: List[String], default: List[String], aliases: List[String] = Nil): Setting[List[String]] =
266280
publish(Setting(name, descr, default, helpArg, Some(choices), aliases = aliases))
267281

282+
def MultiChoiceHelpSetting(name: String, helpArg: String, descr: String, choices: List[ChoiceWithHelp[String]], default: List[ChoiceWithHelp[String]], aliases: List[String] = Nil): Setting[List[ChoiceWithHelp[String]]] =
283+
publish(Setting(name, descr, default, helpArg, Some(choices), aliases = aliases))
284+
268285
def IntSetting(name: String, descr: String, default: Int, aliases: List[String] = Nil): Setting[Int] =
269286
publish(Setting(name, descr, default, aliases = aliases))
270287

0 commit comments

Comments
 (0)