Skip to content

Commit fa45bd3

Browse files
committed
Refine purity check
Refine "a pure expression does nothing in statement position" warning. It should not be issued if the expression has type Unit. Otherwise `()` will trigger a warning.
1 parent 7820b5f commit fa45bd3

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
17091709
traverse(stats ++ rest)
17101710
case stat :: rest =>
17111711
val stat1 = typed(stat)(ctx.exprContext(stat, exprOwner))
1712-
if (!ctx.isAfterTyper && isPureExpr(stat1))
1712+
if (!ctx.isAfterTyper && isPureExpr(stat1) && !stat1.tpe.isRef(defn.UnitClass))
17131713
ctx.warning(em"a pure expression does nothing in statement position", stat.pos)
17141714
buf += stat1
17151715
traverse(rest)

tests/neg/customArgs/xfatalWarnings.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ object xfatalWarnings {
44
opt match { // error when running with -Xfatal-warnings
55
case None =>
66
}
7-
}
7+
8+
object Test {
9+
while (true) {} // should be ok. no "pure expression does nothing in statement position" issued.
10+
}
11+
}

0 commit comments

Comments
 (0)