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. --- test/files/continuations-neg/trycatch2.scala | 6 +++--- test/files/neg/catch-all.check | 10 ++++++++++ test/files/neg/catch-all.flags | 1 + test/files/neg/catch-all.scala | 19 +++++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 test/files/neg/catch-all.check create mode 100644 test/files/neg/catch-all.flags create mode 100644 test/files/neg/catch-all.scala (limited to 'test') diff --git a/test/files/continuations-neg/trycatch2.scala b/test/files/continuations-neg/trycatch2.scala index d61419169b..d329a3b530 100644 --- a/test/files/continuations-neg/trycatch2.scala +++ b/test/files/continuations-neg/trycatch2.scala @@ -12,7 +12,7 @@ object Test { fatal[Int] cpsIntStringInt } catch { - case ex => + case ex: Throwable => cpsIntStringInt } @@ -20,7 +20,7 @@ object Test { fatal[Int] cpsIntStringInt } catch { - case ex => + case ex: Throwable => cpsIntStringInt } @@ -30,4 +30,4 @@ object Test { println(reset { foo2; "3" }) } -} \ No newline at end of file +} diff --git a/test/files/neg/catch-all.check b/test/files/neg/catch-all.check new file mode 100644 index 0000000000..ab3d28777d --- /dev/null +++ b/test/files/neg/catch-all.check @@ -0,0 +1,10 @@ +catch-all.scala:2: error: This catches all Throwables. If this is really intended, use `case _ : Throwable` to clear this warning. + try { "warn" } catch { case _ => } + ^ +catch-all.scala:4: error: This catches all Throwables. If this is really intended, use `case x : Throwable` to clear this warning. + try { "warn" } catch { case x => } + ^ +catch-all.scala:6: error: This catches all Throwables. If this is really intended, use `case x : Throwable` to clear this warning. + try { "warn" } catch { case _: RuntimeException => ; case x => } + ^ +three errors found diff --git a/test/files/neg/catch-all.flags b/test/files/neg/catch-all.flags new file mode 100644 index 0000000000..85d8eb2ba2 --- /dev/null +++ b/test/files/neg/catch-all.flags @@ -0,0 +1 @@ +-Xfatal-warnings diff --git a/test/files/neg/catch-all.scala b/test/files/neg/catch-all.scala new file mode 100644 index 0000000000..35a6d7af91 --- /dev/null +++ b/test/files/neg/catch-all.scala @@ -0,0 +1,19 @@ +object CatchAll { + try { "warn" } catch { case _ => } + + try { "warn" } catch { case x => } + + try { "warn" } catch { case _: RuntimeException => ; case x => } + + try { "okay" } catch { case _: Throwable => } + + try { "okay" } catch { case _: Exception => } + + try { "okay" } catch { case okay: Throwable => } + + try { "okay" } catch { case okay: Exception => } + + try { "okay" } catch { case _ if "".isEmpty => } + + "okay" match { case _ => "" } +} -- cgit v1.2.3