Skip to content

Commit cd92f06

Browse files
Backport "Add the -Wall option that enables all warnings (Plan B)" to 3.5.2 (#21495)
Backports #20577 to the 3.5.2 branch. PR submitted by the release tooling. [skip ci]
2 parents 6b68e24 + a0be090 commit cd92f06

File tree

14 files changed

+124
-35
lines changed

14 files changed

+124
-35
lines changed

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

+40-26
Original file line numberDiff line numberDiff line change
@@ -158,49 +158,53 @@ private sealed trait WarningSettings:
158158

159159
val Whelp: Setting[Boolean] = BooleanSetting(WarningSetting, "W", "Print a synopsis of warning options.")
160160
val XfatalWarnings: Setting[Boolean] = BooleanSetting(WarningSetting, "Werror", "Fail the compilation if there are any warnings.", aliases = List("-Xfatal-warnings"))
161-
val WvalueDiscard: Setting[Boolean] = BooleanSetting(WarningSetting, "Wvalue-discard", "Warn when non-Unit expression results are unused.")
162-
val WNonUnitStatement = BooleanSetting(WarningSetting, "Wnonunit-statement", "Warn when block statements are non-Unit expressions.")
163-
val WenumCommentDiscard = BooleanSetting(WarningSetting, "Wenum-comment-discard", "Warn when a comment ambiguously assigned to multiple enum cases is discarded.")
164-
val WimplausiblePatterns = BooleanSetting(WarningSetting, "Wimplausible-patterns", "Warn if comparison with a pattern value looks like it might always fail.")
165-
val WunstableInlineAccessors = BooleanSetting(WarningSetting, "WunstableInlineAccessors", "Warn an inline methods has references to non-stable binary APIs.")
166-
val Wunused: Setting[List[ChoiceWithHelp[String]]] = MultiChoiceHelpSetting(
161+
val Wall: Setting[Boolean] = BooleanSetting(WarningSetting, "Wall", "Enable all warning settings.")
162+
private val WvalueDiscard: Setting[Boolean] = BooleanSetting(WarningSetting, "Wvalue-discard", "Warn when non-Unit expression results are unused.")
163+
private val WNonUnitStatement = BooleanSetting(WarningSetting, "Wnonunit-statement", "Warn when block statements are non-Unit expressions.")
164+
private val WenumCommentDiscard = BooleanSetting(WarningSetting, "Wenum-comment-discard", "Warn when a comment ambiguously assigned to multiple enum cases is discarded.")
165+
private val WimplausiblePatterns = BooleanSetting(WarningSetting, "Wimplausible-patterns", "Warn if comparison with a pattern value looks like it might always fail.")
166+
private val WunstableInlineAccessors = BooleanSetting(WarningSetting, "WunstableInlineAccessors", "Warn an inline methods has references to non-stable binary APIs.")
167+
private val Wunused: Setting[List[ChoiceWithHelp[String]]] = MultiChoiceHelpSetting(
167168
WarningSetting,
168169
name = "Wunused",
169170
helpArg = "warning",
170171
descr = "Enable or disable specific `unused` warnings",
171172
choices = List(
172173
ChoiceWithHelp("nowarn", ""),
173-
ChoiceWithHelp("all",""),
174+
ChoiceWithHelp("all", ""),
174175
ChoiceWithHelp(
175176
name = "imports",
176177
description = "Warn if an import selector is not referenced.\n" +
177178
"NOTE : overrided by -Wunused:strict-no-implicit-warn"),
178-
ChoiceWithHelp("privates","Warn if a private member is unused"),
179-
ChoiceWithHelp("locals","Warn if a local definition is unused"),
180-
ChoiceWithHelp("explicits","Warn if an explicit parameter is unused"),
181-
ChoiceWithHelp("implicits","Warn if an implicit parameter is unused"),
182-
ChoiceWithHelp("params","Enable -Wunused:explicits,implicits"),
183-
ChoiceWithHelp("linted","Enable -Wunused:imports,privates,locals,implicits"),
184-
ChoiceWithHelp(
185-
name = "strict-no-implicit-warn",
186-
description = "Same as -Wunused:import, only for imports of explicit named members.\n" +
187-
"NOTE : This overrides -Wunused:imports and NOT set by -Wunused:all"
188-
),
189-
// ChoiceWithHelp("patvars","Warn if a variable bound in a pattern is unused"),
190-
ChoiceWithHelp(
191-
name = "unsafe-warn-patvars",
192-
description = "(UNSAFE) Warn if a variable bound in a pattern is unused.\n" +
193-
"This warning can generate false positive, as warning cannot be\n" +
194-
"suppressed yet."
195-
)
179+
ChoiceWithHelp("privates", "Warn if a private member is unused"),
180+
ChoiceWithHelp("locals", "Warn if a local definition is unused"),
181+
ChoiceWithHelp("explicits", "Warn if an explicit parameter is unused"),
182+
ChoiceWithHelp("implicits", "Warn if an implicit parameter is unused"),
183+
ChoiceWithHelp("params", "Enable -Wunused:explicits,implicits"),
184+
ChoiceWithHelp("linted", "Enable -Wunused:imports,privates,locals,implicits"),
185+
ChoiceWithHelp(
186+
name = "strict-no-implicit-warn",
187+
description = "Same as -Wunused:import, only for imports of explicit named members.\n" +
188+
"NOTE : This overrides -Wunused:imports and NOT set by -Wunused:all"
189+
),
190+
// ChoiceWithHelp("patvars","Warn if a variable bound in a pattern is unused"),
191+
ChoiceWithHelp(
192+
name = "unsafe-warn-patvars",
193+
description = "(UNSAFE) Warn if a variable bound in a pattern is unused.\n" +
194+
"This warning can generate false positive, as warning cannot be\n" +
195+
"suppressed yet."
196+
)
196197
),
197198
default = Nil
198199
)
199200
object WunusedHas:
200201
def isChoiceSet(s: String)(using Context) = Wunused.value.pipe(us => us.contains(s))
201-
def allOr(s: String)(using Context) = Wunused.value.pipe(us => us.contains("all") || us.contains(s))
202+
def allOr(s: String)(using Context) = Wall.value || Wunused.value.pipe(us => us.contains("all") || us.contains(s))
202203
def nowarn(using Context) = allOr("nowarn")
203204

205+
// Is any choice set for -Wunused?
206+
def any(using Context): Boolean = Wall.value || Wunused.value.nonEmpty
207+
204208
// overrided by strict-no-implicit-warn
205209
def imports(using Context) =
206210
(allOr("imports") || allOr("linted")) && !(strictNoImplicitWarn)
@@ -298,6 +302,16 @@ private sealed trait WarningSettings:
298302

299303
val WcheckInit: Setting[Boolean] = BooleanSetting(WarningSetting, "Wsafe-init", "Ensure safe initialization of objects.")
300304

305+
object Whas:
306+
def allOr(s: Setting[Boolean])(using Context): Boolean =
307+
Wall.value || s.value
308+
def valueDiscard(using Context): Boolean = allOr(WvalueDiscard)
309+
def nonUnitStatement(using Context): Boolean = allOr(WNonUnitStatement)
310+
def enumCommentDiscard(using Context): Boolean = allOr(WenumCommentDiscard)
311+
def implausiblePatterns(using Context): Boolean = allOr(WimplausiblePatterns)
312+
def unstableInlineAccessors(using Context): Boolean = allOr(WunstableInlineAccessors)
313+
def checkInit(using Context): Boolean = allOr(WcheckInit)
314+
301315
/** -X "Extended" or "Advanced" settings */
302316
private sealed trait XSettings:
303317
self: SettingGroup =>

compiler/src/dotty/tools/dotc/core/Symbols.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ object Symbols extends SymUtils {
8484
ctx.settings.YretainTrees.value ||
8585
denot.owner.isTerm || // no risk of leaking memory after a run for these
8686
denot.isOneOf(InlineOrProxy) || // need to keep inline info
87-
ctx.settings.WcheckInit.value || // initialization check
87+
ctx.settings.Whas.checkInit || // initialization check
8888
ctx.settings.YcheckInitGlobal.value
8989

9090
/** The last denotation of this symbol */

compiler/src/dotty/tools/dotc/inlines/PrepareInlineable.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ object PrepareInlineable {
9191
postTransform(super.transform(preTransform(tree)))
9292

9393
protected def checkUnstableAccessor(accessedTree: Tree, accessor: Symbol)(using Context): Unit =
94-
if ctx.settings.WunstableInlineAccessors.value then
94+
if ctx.settings.Whas.unstableInlineAccessors then
9595
val accessorTree = accessorDef(accessor, accessedTree.symbol)
9696
report.warning(reporting.UnstableInlineAccessor(accessedTree.symbol, accessorTree), accessedTree)
9797
}

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -4083,7 +4083,7 @@ object Parsers {
40834083
if (in.token == COMMA) {
40844084
in.nextToken()
40854085
val ids = commaSeparated(() => termIdent())
4086-
if ctx.settings.WenumCommentDiscard.value then
4086+
if ctx.settings.Whas.enumCommentDiscard then
40874087
in.getDocComment(start).foreach: comm =>
40884088
warning(
40894089
em"""Ambiguous Scaladoc comment on multiple cases is ignored.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
5858

5959
override def isRunnable(using Context): Boolean =
6060
super.isRunnable &&
61-
ctx.settings.Wunused.value.nonEmpty &&
61+
ctx.settings.WunusedHas.any &&
6262
!ctx.isJava
6363

6464
// ========== SETUP ============

compiler/src/dotty/tools/dotc/transform/init/Checker.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Checker extends Phase:
2929
override val runsAfter = Set(Pickler.name)
3030

3131
override def isEnabled(using Context): Boolean =
32-
super.isEnabled && (ctx.settings.WcheckInit.value || ctx.settings.YcheckInitGlobal.value)
32+
super.isEnabled && (ctx.settings.Whas.checkInit || ctx.settings.YcheckInitGlobal.value)
3333

3434
def traverse(traverser: InitTreeTraverser)(using Context): Boolean = monitor(phaseName):
3535
val unit = ctx.compilationUnit
@@ -50,7 +50,7 @@ class Checker extends Phase:
5050
cancellable {
5151
val classes = traverser.getClasses()
5252

53-
if ctx.settings.WcheckInit.value then
53+
if ctx.settings.Whas.checkInit then
5454
Semantic.checkClasses(classes)(using checkCtx)
5555

5656
if ctx.settings.YcheckInitGlobal.value then

compiler/src/dotty/tools/dotc/typer/Linter.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ object Linter:
5555
&& !isJavaApplication(t) // Java methods are inherently side-effecting
5656
// && !treeInfo.hasExplicitUnit(t) // suppressed by explicit expr: Unit // TODO Should explicit `: Unit` be added as warning suppression?
5757

58-
if ctx.settings.WNonUnitStatement.value && !ctx.isAfterTyper && checkInterestingShapes(t) then
58+
if ctx.settings.Whas.nonUnitStatement && !ctx.isAfterTyper && checkInterestingShapes(t) then
5959
val where = t match
6060
case Block(_, res) => res
6161
case If(_, thenpart, Literal(Constant(()))) =>
@@ -119,7 +119,7 @@ object Linter:
119119
// still compute `canEqual(A & B, B & A) = true`.
120120
canEqual(a, b.tp1) || canEqual(a, b.tp2)
121121

122-
if ctx.settings.WimplausiblePatterns.value && !canEqual(pat.tpe, selType) then
122+
if ctx.settings.Whas.implausiblePatterns && !canEqual(pat.tpe, selType) then
123123
report.warning(ImplausiblePatternWarning(pat, selType), pat.srcPos)
124124
end warnOnImplausiblePattern
125125

compiler/src/dotty/tools/dotc/typer/Typer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -4496,7 +4496,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
44964496
// so will take the code path that decides on inlining
44974497
val tree1 = adapt(tree, WildcardType, locked)
44984498
checkStatementPurity(tree1)(tree, ctx.owner, isUnitExpr = true)
4499-
if (!ctx.isAfterTyper && !tree.isInstanceOf[Inlined] && ctx.settings.WvalueDiscard.value && !isThisTypeResult(tree)) {
4499+
if (!ctx.isAfterTyper && !tree.isInstanceOf[Inlined] && ctx.settings.Whas.valueDiscard && !isThisTypeResult(tree)) {
45004500
report.warning(ValueDiscarding(tree.tpe), tree.srcPos)
45014501
}
45024502
return tpd.Block(tree1 :: Nil, unitLiteral)

tests/warn/i18559a.check

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- [E198] Unused Symbol Warning: tests/warn/i18559a.scala:4:28 ---------------------------------------------------------
2+
4 | import collection.mutable.Set // warn
3+
| ^^^
4+
| unused import
5+
-- [E198] Unused Symbol Warning: tests/warn/i18559a.scala:8:8 ----------------------------------------------------------
6+
8 | val x = 1 // warn
7+
| ^
8+
| unused local definition
9+
-- [E198] Unused Symbol Warning: tests/warn/i18559a.scala:11:26 --------------------------------------------------------
10+
11 | import SomeGivenImports.given // warn
11+
| ^^^^^
12+
| unused import

tests/warn/i18559a.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//> using options -Wall
2+
// This test checks that -Wall turns on -Wunused:all if -Wunused is not set
3+
object FooImportUnused:
4+
import collection.mutable.Set // warn
5+
6+
object FooUnusedLocal:
7+
def test(): Unit =
8+
val x = 1 // warn
9+
10+
object FooGivenUnused:
11+
import SomeGivenImports.given // warn
12+
13+
object SomeGivenImports:
14+
given Int = 0
15+
given String = "foo"

tests/warn/i18559b.check

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Warning: tests/warn/i18559b.scala:8:6 -------------------------------------------------------------------------------
2+
8 | val localFile: String = s"${url.##}.tmp" // warn
3+
| ^
4+
| Access non-initialized value localFile. Calling trace:
5+
| ├── class RemoteFile(url: String) extends AbstractFile: [ i18559b.scala:7 ]
6+
| │ ^
7+
| ├── abstract class AbstractFile: [ i18559b.scala:3 ]
8+
| │ ^
9+
| ├── val extension: String = name.substring(4) [ i18559b.scala:5 ]
10+
| │ ^^^^
11+
| └── def name: String = localFile [ i18559b.scala:9 ]
12+
| ^^^^^^^^^

tests/warn/i18559b.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//> using options -Wall
2+
// This test checks that -Wall turns on -Wsafe-init
3+
abstract class AbstractFile:
4+
def name: String
5+
val extension: String = name.substring(4)
6+
7+
class RemoteFile(url: String) extends AbstractFile:
8+
val localFile: String = s"${url.##}.tmp" // warn
9+
def name: String = localFile

tests/warn/i18559c.check

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- [E198] Unused Symbol Warning: tests/warn/i18559c.scala:4:28 ---------------------------------------------------------
2+
4 | import collection.mutable.Set // warn
3+
| ^^^
4+
| unused import
5+
-- [E198] Unused Symbol Warning: tests/warn/i18559c.scala:8:8 ----------------------------------------------------------
6+
8 | val x = 1 // warn
7+
| ^
8+
| unused local definition
9+
-- [E198] Unused Symbol Warning: tests/warn/i18559c.scala:11:26 --------------------------------------------------------
10+
11 | import SomeGivenImports.given // warn
11+
| ^^^^^
12+
| unused import

tests/warn/i18559c.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//> using options -Wall -Wunused:locals
2+
// This test checks that -Wall overrides -Wunused:... if it is already set
3+
object FooImportUnused:
4+
import collection.mutable.Set // warn
5+
6+
object FooUnusedLocal:
7+
def test(): Unit =
8+
val x = 1 // warn
9+
10+
object FooGivenUnused:
11+
import SomeGivenImports.given // warn
12+
13+
object SomeGivenImports:
14+
given Int = 0
15+
given String = "foo"

0 commit comments

Comments
 (0)