diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-06-22 00:42:39 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-06-22 00:42:39 -0700 |
commit | cfc0e18dc55b33274c1275c192ade4a8d707d635 (patch) | |
tree | ed500a8374b7911fe3b6a7443784764fcd231022 | |
parent | ff016002738580a7de11977c91e0061dbcf270ad (diff) | |
parent | 7d8527b40a3bd6d93cf155b39ee50577751f3e91 (diff) | |
download | scala-cfc0e18dc55b33274c1275c192ade4a8d707d635.tar.gz scala-cfc0e18dc55b33274c1275c192ade4a8d707d635.tar.bz2 scala-cfc0e18dc55b33274c1275c192ade4a8d707d635.zip |
Merge pull request #752 from retronym/topic/warn-catch-all-4
SI-2807 Resurrect and refine the promiscuous catch warning.
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Typers.scala | 10 | ||||
-rw-r--r-- | test/files/continuations-neg/trycatch2.scala | 6 | ||||
-rw-r--r-- | test/files/neg/catch-all.check | 10 | ||||
-rw-r--r-- | test/files/neg/catch-all.flags | 1 | ||||
-rw-r--r-- | test/files/neg/catch-all.scala | 19 |
5 files changed, 43 insertions, 3 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index b12ca4f0b4..69d3fd7f47 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -4863,6 +4863,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) 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 _ => "" } +} |