Skip to content

Commit 84232bd

Browse files
committed
Add -Wall option
1 parent 62f0324 commit 84232bd

File tree

4 files changed

+146
-6
lines changed

4 files changed

+146
-6
lines changed

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,12 @@ private sealed trait WarningSettings:
163163
self: SettingGroup =>
164164

165165
val Whelp: Setting[Boolean] = BooleanSetting("-W", "Print a synopsis of warning options.")
166+
val Wall: Setting[Boolean] = BooleanSetting("-Wall", "Enable all warnings")
166167
val XfatalWarnings: Setting[Boolean] = BooleanSetting("-Werror", "Fail the compilation if there are any warnings.", aliases = List("-Xfatal-warnings"))
167-
val WvalueDiscard: Setting[Boolean] = BooleanSetting("-Wvalue-discard", "Warn when non-Unit expression results are unused.")
168-
val WNonUnitStatement = BooleanSetting("-Wnonunit-statement", "Warn when block statements are non-Unit expressions.")
169-
val WimplausiblePatterns = BooleanSetting("-Wimplausible-patterns", "Warn if comparison with a pattern value looks like it might always fail.")
170-
val WunstableInlineAccessors = BooleanSetting("-WunstableInlineAccessors", "Warn an inline methods has references to non-stable binary APIs.")
168+
val WvalueDiscard: Setting[Boolean] = BooleanSetting("-Wvalue-discard", "Warn when non-Unit expression results are unused.").enableWith(Wall)
169+
val WNonUnitStatement = BooleanSetting("-Wnonunit-statement", "Warn when block statements are non-Unit expressions.").enableWith(Wall)
170+
val WimplausiblePatterns = BooleanSetting("-Wimplausible-patterns", "Warn if comparison with a pattern value looks like it might always fail.").enableWith(Wall)
171+
val WunstableInlineAccessors = BooleanSetting("-WunstableInlineAccessors", "Warn an inline methods has references to non-stable binary APIs.").enableWith(Wall)
171172
val Wunused: Setting[List[ChoiceWithHelp[String]]] = MultiChoiceHelpSetting(
172173
name = "-Wunused",
173174
helpArg = "warning",
@@ -199,7 +200,7 @@ private sealed trait WarningSettings:
199200
)
200201
),
201202
default = Nil
202-
)
203+
).enableWith(Wall, List(ChoiceWithHelp("all", "")))
203204
object WunusedHas:
204205
def isChoiceSet(s: String)(using Context) = Wunused.value.pipe(us => us.contains(s))
205206
def allOr(s: String)(using Context) = Wunused.value.pipe(us => us.contains("all") || us.contains(s))

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,27 @@ object Settings:
6464
depends: List[(Setting[?], Any)] = Nil,
6565
ignoreInvalidArgs: Boolean = false,
6666
propertyClass: Option[Class[?]] = None)(private[Settings] val idx: Int) {
67+
68+
private var enabledWith: Option[Setting[Boolean]] = None
69+
private var enabledWithValue: T = _
6770

6871
private var changed: Boolean = false
6972

70-
def valueIn(state: SettingsState): T = state.value(idx).asInstanceOf[T]
73+
def valueIn(state: SettingsState): T =
74+
enabledWith.filter(_.valueIn(state)).map(_ => enabledWithValue)
75+
.getOrElse(state.value(idx).asInstanceOf[T])
76+
77+
def enableWith(setting: Setting[Boolean])(using T =:= Boolean): Setting[T] = {
78+
enabledWith = Some(setting)
79+
enabledWithValue = true.asInstanceOf[T]
80+
this
81+
}
82+
83+
def enableWith[A](setting: Setting[Boolean], value: T): Setting[T] = {
84+
enabledWith = Some(setting)
85+
enabledWithValue = value
86+
this
87+
}
7188

7289
def updateIn(state: SettingsState, x: Any): SettingsState = x match
7390
case _: T => state.update(idx, x)

tests/warn/i18559.check

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
-- [E176] Potential Issue Warning: tests/warn/i18559.scala:63:4 --------------------------------------------------------
2+
63 | improved // warn
3+
| ^^^^^^^^
4+
| unused value of type (improved : => scala.concurrent.Future[Int])
5+
-- [E175] Potential Issue Warning: tests/warn/i18559.scala:51:35 -------------------------------------------------------
6+
51 | firstThing().map(_ => secondThing()) // warn
7+
| ^^^^^^^^^^^^^
8+
| discarded non-Unit value of type Either[Failed, Unit]
9+
-- [E175] Potential Issue Warning: tests/warn/i18559.scala:54:35 -------------------------------------------------------
10+
54 | firstThing().map(_ => secondThing()) // warn
11+
| ^^^^^^^^^^^^^
12+
| discarded non-Unit value of type Either[Failed, Unit]
13+
-- Warning: tests/warn/i18559.scala:4:6 --------------------------------------------------------------------------------
14+
4 | var e3 = 2 // warn
15+
| ^^
16+
| unused local definition
17+
-- Warning: tests/warn/i18559.scala:8:28 -------------------------------------------------------------------------------
18+
8 | import collection.mutable.Set // warn
19+
| ^^^
20+
| unused import
21+
-- Warning: tests/warn/i18559.scala:9:33 -------------------------------------------------------------------------------
22+
9 | import collection.mutable.{Map => MutMap} // warn
23+
| ^^^^^^^^^^^^^
24+
| unused import
25+
-- Warning: tests/warn/i18559.scala:10:28 ------------------------------------------------------------------------------
26+
10 | import collection.mutable._ // warn
27+
| ^
28+
| unused import
29+
-- Warning: tests/warn/i18559.scala:13:28 ------------------------------------------------------------------------------
30+
13 | import collection.mutable._ // warn
31+
| ^
32+
| unused import
33+
-- [E030] Match case Unreachable Warning: tests/warn/i18559.scala:31:10 ------------------------------------------------
34+
31 | case Sum(a@S(_),Z) => Z // warn
35+
| ^^^^^^^^^^^^^
36+
| Unreachable case
37+
-- [E030] Match case Unreachable Warning: tests/warn/i18559.scala:32:10 ------------------------------------------------
38+
32 | case Sum(a@S(_),Z) => a // warn unreachable
39+
| ^^^^^^^^^^^^^
40+
| Unreachable case
41+
-- [E030] Match case Unreachable Warning: tests/warn/i18559.scala:33:10 ------------------------------------------------
42+
33 | case Sum(a@S(b@S(_)), Z) => a // warn
43+
| ^^^^^^^^^^^^^^^^^^^
44+
| Unreachable case
45+
-- [E030] Match case Unreachable Warning: tests/warn/i18559.scala:34:10 ------------------------------------------------
46+
34 | case Sum(a@S(b@S(_)), Z) => a // warn
47+
| ^^^^^^^^^^^^^^^^^^^
48+
| Unreachable case
49+
-- [E030] Match case Unreachable Warning: tests/warn/i18559.scala:35:10 ------------------------------------------------
50+
35 | case Sum(a@S(b@(S(_))), Z) => Sum(a,b) // warn unreachable
51+
| ^^^^^^^^^^^^^^^^^^^^^
52+
| Unreachable case
53+
-- [E121] Pattern Match Warning: tests/warn/i18559.scala:37:7 ----------------------------------------------------------
54+
37 | case _ => Z // warn unreachable
55+
| ^
56+
| Unreachable case except for null (if this is intentional, consider writing case null => instead).

tests/warn/i18559.scala

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//> using options -Wall
2+
3+
val b = // OK
4+
var e3 = 2 // warn
5+
1
6+
7+
object FooUnused:
8+
import collection.mutable.Set // warn
9+
import collection.mutable.{Map => MutMap} // warn
10+
import collection.mutable._ // warn
11+
12+
object FooWildcardUnused:
13+
import collection.mutable._ // warn
14+
15+
object Foo:
16+
import collection.mutable.Set // OK
17+
import collection.mutable.{Map => MutMap} // OK
18+
19+
val bar = Set() // OK
20+
val baz = MutMap() // OK
21+
22+
sealed trait Calc
23+
sealed trait Const extends Calc
24+
case class Sum(a: Calc, b: Calc) extends Calc
25+
case class S(pred: Const) extends Const
26+
case object Z extends Const
27+
28+
val a = Sum(S(S(Z)),Z) match {
29+
case Sum(a,Z) => Z // not warn: patvars not enabled by Wall
30+
// case Sum(a @ _,Z) => Z // todo : this should pass in the future
31+
case Sum(a@S(_),Z) => Z // warn
32+
case Sum(a@S(_),Z) => a // warn unreachable
33+
case Sum(a@S(b@S(_)), Z) => a // warn
34+
case Sum(a@S(b@S(_)), Z) => a // warn
35+
case Sum(a@S(b@(S(_))), Z) => Sum(a,b) // warn unreachable
36+
case Sum(_,_) => Z // OK
37+
case _ => Z // warn unreachable
38+
}
39+
import scala.util.{Either, Right, Left}
40+
import scala.collection.mutable
41+
42+
case class Failed(msg: String)
43+
44+
def firstThing(): Either[Failed, Unit] =
45+
Right(())
46+
47+
def secondThing(): Either[Failed, Unit] =
48+
Left(Failed("whoops you should have flatMapped me"))
49+
50+
def singleExpr(): Either[Failed, Unit] =
51+
firstThing().map(_ => secondThing()) // warn
52+
53+
def block(): Either[Failed, Unit] = {
54+
firstThing().map(_ => secondThing()) // warn
55+
}
56+
57+
class C {
58+
import concurrent._
59+
import ExecutionContext.Implicits._
60+
def c = {
61+
def improved = Future(42)
62+
def stale = Future(27)
63+
improved // warn
64+
stale
65+
}
66+
}

0 commit comments

Comments
 (0)