From 7d8527b40a3bd6d93cf155b39ee50577751f3e91 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sat, 16 Jun 2012 23:35:06 +0200 Subject: SI-2807 Resurrect and refine the promiscuous catch warning. The previous incarnation didn't survive 4fb3473. This version can be cleared by using a typed pattern: `catch { case _: Throwable => }`. This is motivated by the recent appearance of such a catch in `util.Try`, and by battle scars left by one too many processes bravely but stupidly catching and logging OutOfMemoryErrors. -Y status has been skipped: this warning is enabled by default and can only be silenced with use of a typed pattern. --- src/compiler/scala/tools/nsc/typechecker/Typers.scala | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 2bdae4164a..a23bc35e77 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -4825,6 +4825,16 @@ trait Typers extends Modes with Adaptations with Tags { case Try(block, catches, finalizer) => var block1 = typed(block, pt) var catches1 = typedCases(catches, ThrowableClass.tpe, pt) + + for (cdef <- catches1 if cdef.guard.isEmpty) { + def warn(name: Name) = context.warning(cdef.pat.pos, s"This catches all Throwables. If this is really intended, use `case ${name.decoded} : Throwable` to clear this warning.") + cdef.pat match { + case Bind(name, Ident(_)) => warn(name) + case Ident(name) => warn(name) + case _ => + } + } + val finalizer1 = if (finalizer.isEmpty) finalizer else typed(finalizer, UnitClass.tpe) val (owntype, needAdapt) = ptOrLub(block1.tpe :: (catches1 map (_.tpe)), pt) -- cgit v1.2.3