diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala index 027c8ff0411c..01bfd755f041 100644 --- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala +++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala @@ -1788,7 +1788,6 @@ abstract class RefChecks extends Transform { ) // begin checkInterestingResultInStatement settings.warnNonUnitStatement.value && checkInterestingShapes(t) && { - val msg = "unused value" val where = t match { case Block(_, res) => res case If(_, thenpart, Literal(Constant(()))) => @@ -1798,6 +1797,7 @@ abstract class RefChecks extends Transform { } case _ => t } + def msg = s"unused value of type ${where.tpe} (add `: Unit` to discard silently)" refchecksWarning(where.pos, msg, WarningCategory.OtherPureStatement) true } diff --git a/test/files/neg/nonunit-if.check b/test/files/neg/nonunit-if.check index 6d9e0ef63e69..369a86e621c3 100644 --- a/test/files/neg/nonunit-if.check +++ b/test/files/neg/nonunit-if.check @@ -4,52 +4,52 @@ nonunit-if.scala:58: warning: discarded non-Unit value nonunit-if.scala:62: warning: discarded non-Unit value f(a) // warn, check is on ^ -nonunit-if.scala:13: warning: unused value +nonunit-if.scala:13: warning: unused value of type scala.concurrent.Future[Int] (add `: Unit` to discard silently) improved // warn ^ -nonunit-if.scala:20: warning: unused value +nonunit-if.scala:20: warning: unused value of type String (add `: Unit` to discard silently) new E().toString // warn ^ -nonunit-if.scala:26: warning: unused value +nonunit-if.scala:26: warning: unused value of type scala.concurrent.Future[Int] (add `: Unit` to discard silently) Future(42) // warn ^ -nonunit-if.scala:30: warning: unused value +nonunit-if.scala:30: warning: unused value of type K (add `: Unit` to discard silently) copy() // warn ^ -nonunit-if.scala:37: warning: unused value +nonunit-if.scala:37: warning: unused value of type List[Int] (add `: Unit` to discard silently) 27 +: xs // warn ^ nonunit-if.scala:44: warning: a pure expression does nothing in statement position; multiline expressions may require enclosing parentheses null // warn for purity ^ -nonunit-if.scala:58: warning: unused value +nonunit-if.scala:58: warning: unused value of type U (add `: Unit` to discard silently) if (!isEmpty) f(a) // warn, check is on ^ -nonunit-if.scala:62: warning: unused value +nonunit-if.scala:62: warning: unused value of type Boolean (add `: Unit` to discard silently) f(a) // warn, check is on ^ -nonunit-if.scala:73: warning: unused value +nonunit-if.scala:73: warning: unused value of type U (add `: Unit` to discard silently) if (!fellback) action(z) // warn, check is on ^ -nonunit-if.scala:81: warning: unused value +nonunit-if.scala:81: warning: unused value of type Int (add `: Unit` to discard silently) g // warn, check is on ^ -nonunit-if.scala:79: warning: unused value +nonunit-if.scala:79: warning: unused value of type Int (add `: Unit` to discard silently) g // warn block statement ^ -nonunit-if.scala:86: warning: unused value +nonunit-if.scala:86: warning: unused value of type Int (add `: Unit` to discard silently) g // warn ^ -nonunit-if.scala:84: warning: unused value +nonunit-if.scala:84: warning: unused value of type Int (add `: Unit` to discard silently) g // warn ^ -nonunit-if.scala:96: warning: unused value +nonunit-if.scala:96: warning: unused value of type Int (add `: Unit` to discard silently) if (b) { // warn, at least one branch looks interesting ^ -nonunit-if.scala:116: warning: unused value +nonunit-if.scala:116: warning: unused value of type scala.collection.mutable.LinkedHashSet[A] (add `: Unit` to discard silently) set += a // warn because cannot know whether the `set` was supposed to be consumed or assigned ^ -nonunit-if.scala:146: warning: unused value +nonunit-if.scala:146: warning: unused value of type String (add `: Unit` to discard silently) while (it.hasNext) it.next() // warn ^ error: No warnings can be incurred under -Werror. diff --git a/test/files/neg/nonunit-statement.check b/test/files/neg/nonunit-statement.check index f98bcd3bc607..b15ae34fb7fd 100644 --- a/test/files/neg/nonunit-statement.check +++ b/test/files/neg/nonunit-statement.check @@ -1,37 +1,37 @@ -nonunit-statement.scala:13: warning: unused value +nonunit-statement.scala:13: warning: unused value of type scala.concurrent.Future[Int] (add `: Unit` to discard silently) improved // warn ^ -nonunit-statement.scala:20: warning: unused value +nonunit-statement.scala:20: warning: unused value of type String (add `: Unit` to discard silently) new E().toString // warn ^ -nonunit-statement.scala:26: warning: unused value +nonunit-statement.scala:26: warning: unused value of type scala.concurrent.Future[Int] (add `: Unit` to discard silently) Future(42) // warn ^ -nonunit-statement.scala:30: warning: unused value +nonunit-statement.scala:30: warning: unused value of type K (add `: Unit` to discard silently) copy() // warn ^ -nonunit-statement.scala:37: warning: unused value +nonunit-statement.scala:37: warning: unused value of type List[Int] (add `: Unit` to discard silently) 27 +: xs // warn ^ nonunit-statement.scala:44: warning: a pure expression does nothing in statement position; multiline expressions may require enclosing parentheses null // warn for purity ^ -nonunit-statement.scala:79: warning: unused value +nonunit-statement.scala:79: warning: unused value of type Int (add `: Unit` to discard silently) g // warn block statement ^ -nonunit-statement.scala:86: warning: unused value +nonunit-statement.scala:86: warning: unused value of type Int (add `: Unit` to discard silently) g // warn ^ -nonunit-statement.scala:84: warning: unused value +nonunit-statement.scala:84: warning: unused value of type Int (add `: Unit` to discard silently) g // warn ^ -nonunit-statement.scala:96: warning: unused value +nonunit-statement.scala:96: warning: unused value of type Int (add `: Unit` to discard silently) if (b) { // warn, at least one branch looks interesting ^ -nonunit-statement.scala:116: warning: unused value +nonunit-statement.scala:116: warning: unused value of type scala.collection.mutable.LinkedHashSet[A] (add `: Unit` to discard silently) set += a // warn because cannot know whether the `set` was supposed to be consumed or assigned ^ -nonunit-statement.scala:146: warning: unused value +nonunit-statement.scala:146: warning: unused value of type String (add `: Unit` to discard silently) while (it.hasNext) it.next() // warn ^ error: No warnings can be incurred under -Werror.