From a3315cdcf6ca2714e563eb813b645e267ba2ea5a Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sat, 30 Jun 2012 09:13:05 +0200 Subject: SI-2807 Avoid catch all warning for Stable Id patterns --- .../scala/tools/nsc/typechecker/Typers.scala | 5 ++-- test/files/neg/catch-all.check | 12 ++++----- test/files/neg/catch-all.scala | 30 +++++++++++++++------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index d251109dc4..25857773fe 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -4865,9 +4865,10 @@ trait Typers extends Modes with Adaptations with Tags { 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.") + def unbound(t: Tree) = t.symbol == null || t.symbol == NoSymbol cdef.pat match { - case Bind(name, Ident(_)) => warn(name) - case Ident(name) => warn(name) + case Bind(name, i@Ident(_)) if unbound(i) => warn(name) + case i@Ident(name) if unbound(i) => warn(name) case _ => } } diff --git a/test/files/neg/catch-all.check b/test/files/neg/catch-all.check index ab3d28777d..62f895cc7e 100644 --- a/test/files/neg/catch-all.check +++ b/test/files/neg/catch-all.check @@ -1,10 +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 _ => } - ^ + 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 => } - ^ + 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 => } - ^ + try { "warn" } catch { case _: RuntimeException => ; case x => } + ^ three errors found diff --git a/test/files/neg/catch-all.scala b/test/files/neg/catch-all.scala index 35a6d7af91..c05be77044 100644 --- a/test/files/neg/catch-all.scala +++ b/test/files/neg/catch-all.scala @@ -1,19 +1,31 @@ object CatchAll { - try { "warn" } catch { case _ => } + try { "warn" } catch { case _ => } - try { "warn" } catch { case x => } + try { "warn" } catch { case x => } - try { "warn" } catch { case _: RuntimeException => ; case x => } + try { "warn" } catch { case _: RuntimeException => ; case x => } - try { "okay" } catch { case _: Throwable => } + val t = T - try { "okay" } catch { case _: Exception => } + try { "okay" } catch { case T => } - try { "okay" } catch { case okay: Throwable => } + try { "okay" } catch { case `t` => } - try { "okay" } catch { case okay: Exception => } + try { "okay" } catch { case x @ T => } - try { "okay" } catch { case _ if "".isEmpty => } + try { "okay" } catch { case x @ `t` => } - "okay" match { case _ => "" } + 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 _ => "" } } + +object T extends Throwable -- cgit v1.2.3