From fa45bd3a4ab65598a58bf5264a7f68d2190a4a8c Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 6 Jul 2017 13:28:56 +0200 Subject: [PATCH] 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. --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 2 +- tests/neg/customArgs/xfatalWarnings.scala | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index c8f2c26874ad..abdd2c868b89 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1709,7 +1709,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit traverse(stats ++ rest) case stat :: rest => val stat1 = typed(stat)(ctx.exprContext(stat, exprOwner)) - if (!ctx.isAfterTyper && isPureExpr(stat1)) + if (!ctx.isAfterTyper && isPureExpr(stat1) && !stat1.tpe.isRef(defn.UnitClass)) ctx.warning(em"a pure expression does nothing in statement position", stat.pos) buf += stat1 traverse(rest) diff --git a/tests/neg/customArgs/xfatalWarnings.scala b/tests/neg/customArgs/xfatalWarnings.scala index 86ca7ed8f4dc..862b94039e2a 100644 --- a/tests/neg/customArgs/xfatalWarnings.scala +++ b/tests/neg/customArgs/xfatalWarnings.scala @@ -4,4 +4,8 @@ object xfatalWarnings { opt match { // error when running with -Xfatal-warnings case None => } -} \ No newline at end of file + + object Test { + while (true) {} // should be ok. no "pure expression does nothing in statement position" issued. + } +}