Skip to content

Commit 38d0b82

Browse files
committed
Suppress value discarding warning when ascribed to Unit
1 parent f09a856 commit 38d0b82

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala

+3
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ trait StdAttachments {
197197
*/
198198
case class OriginalTreeAttachment(original: Tree)
199199

200+
/** Marks a Typed tree with Unit tpt. */
201+
case object TypedExpectingUnitAttachment
202+
200203
case class StabilizingDefinitions(vdefs: List[ValDef])
201204
private[this] val StabilizingDefinitionsTag: reflect.ClassTag[StabilizingDefinitions] = reflect.classTag[StabilizingDefinitions]
202205

src/compiler/scala/tools/nsc/typechecker/Typers.scala

+12-2
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,14 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
10571057
case (Apply(Select(receiver, _), _), SingleType(_, sym)) => sym == receiver.symbol
10581058
case _ => false
10591059
}
1060-
if (!isThisTypeResult) context.warning(tree.pos, "discarded non-Unit value")
1060+
def wasTypedExpectingUnit = {
1061+
tree.attachments.contains[TypedExpectingUnitAttachment.type] && {
1062+
tree.attachments.remove[TypedExpectingUnitAttachment.type] // cleanup
1063+
true
1064+
}
1065+
}
1066+
if (!isThisTypeResult && !wasTypedExpectingUnit)
1067+
context.warning(tree.pos, "discarded non-Unit value")
10611068
}
10621069
@inline def warnNumericWiden(): Unit =
10631070
if (!isPastTyper && settings.warnNumericWiden) context.warning(tree.pos, "implicit numeric widening")
@@ -5442,7 +5449,10 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
54425449
}
54435450
case Typed(expr, tpt) =>
54445451
val tpt1 = typedType(tpt, mode) // type the ascribed type first
5445-
val expr1 = typed(expr, mode.onlySticky, tpt1.tpe.deconst) // then type the expression with tpt1 as the expected type
5452+
val exprWithAttachment =
5453+
if (definitions.isUnitType(tpt1.tpe)) expr.updateAttachment(TypedExpectingUnitAttachment)
5454+
else expr
5455+
val expr1 = typed(exprWithAttachment, mode.onlySticky, tpt1.tpe.deconst) // then type the expression with tpt1 as the expected type
54465456
treeCopy.Typed(tree, expr1, tpt1) setType tpt1.tpe
54475457
}
54485458
}

test/files/neg/t9847.check

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ t9847.scala:6: warning: discarded non-Unit value
44
t9847.scala:6: warning: a pure expression does nothing in statement position
55
def f(): Unit = 42
66
^
7-
t9847.scala:7: warning: discarded non-Unit value
8-
def g = (42: Unit)
9-
^
107
t9847.scala:7: warning: a pure expression does nothing in statement position
118
def g = (42: Unit)
129
^
@@ -41,5 +38,5 @@ t9847.scala:24: warning: a pure expression does nothing in statement position; m
4138
class D { 42 ; 17 }
4239
^
4340
error: No warnings can be incurred under -Xfatal-warnings.
44-
14 warnings found
41+
13 warnings found
4542
one error found

test/files/neg/value-discard.check

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
value-discard.scala:6: warning: discarded non-Unit value
2+
mutable.Set[String]().remove("") // expected to warn
3+
^
4+
error: No warnings can be incurred under -Xfatal-warnings.
5+
one warning found
6+
one error found

test/files/neg/value-discard.scala

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// scalac: -Ywarn-value-discard -Xfatal-warnings
2+
final class UnusedTest {
3+
import scala.collection.mutable
4+
5+
def remove(): Unit = {
6+
mutable.Set[String]().remove("") // expected to warn
7+
}
8+
9+
def removeAscribed(): Unit = {
10+
mutable.Set[String]().remove(""): Unit
11+
}
12+
}

0 commit comments

Comments
 (0)