Skip to content

Commit cfc0e18

Browse files
author
Adriaan Moors
committed
Merge pull request scala#752 from retronym/topic/warn-catch-all-4
SI-2807 Resurrect and refine the promiscuous catch warning.
2 parents ff01600 + 7d8527b commit cfc0e18

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4863,6 +4863,16 @@ trait Typers extends Modes with Adaptations with Tags {
48634863
case Try(block, catches, finalizer) =>
48644864
var block1 = typed(block, pt)
48654865
var catches1 = typedCases(catches, ThrowableClass.tpe, pt)
4866+
4867+
for (cdef <- catches1 if cdef.guard.isEmpty) {
4868+
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.")
4869+
cdef.pat match {
4870+
case Bind(name, Ident(_)) => warn(name)
4871+
case Ident(name) => warn(name)
4872+
case _ =>
4873+
}
4874+
}
4875+
48664876
val finalizer1 = if (finalizer.isEmpty) finalizer
48674877
else typed(finalizer, UnitClass.tpe)
48684878
val (owntype, needAdapt) = ptOrLub(block1.tpe :: (catches1 map (_.tpe)), pt)

test/files/continuations-neg/trycatch2.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ object Test {
1212
fatal[Int]
1313
cpsIntStringInt
1414
} catch {
15-
case ex =>
15+
case ex: Throwable =>
1616
cpsIntStringInt
1717
}
1818

1919
def foo2 = try {
2020
fatal[Int]
2121
cpsIntStringInt
2222
} catch {
23-
case ex =>
23+
case ex: Throwable =>
2424
cpsIntStringInt
2525
}
2626

@@ -30,4 +30,4 @@ object Test {
3030
println(reset { foo2; "3" })
3131
}
3232

33-
}
33+
}

test/files/neg/catch-all.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
catch-all.scala:2: error: This catches all Throwables. If this is really intended, use `case _ : Throwable` to clear this warning.
2+
try { "warn" } catch { case _ => }
3+
^
4+
catch-all.scala:4: error: This catches all Throwables. If this is really intended, use `case x : Throwable` to clear this warning.
5+
try { "warn" } catch { case x => }
6+
^
7+
catch-all.scala:6: error: This catches all Throwables. If this is really intended, use `case x : Throwable` to clear this warning.
8+
try { "warn" } catch { case _: RuntimeException => ; case x => }
9+
^
10+
three errors found

test/files/neg/catch-all.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Xfatal-warnings

test/files/neg/catch-all.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
object CatchAll {
2+
try { "warn" } catch { case _ => }
3+
4+
try { "warn" } catch { case x => }
5+
6+
try { "warn" } catch { case _: RuntimeException => ; case x => }
7+
8+
try { "okay" } catch { case _: Throwable => }
9+
10+
try { "okay" } catch { case _: Exception => }
11+
12+
try { "okay" } catch { case okay: Throwable => }
13+
14+
try { "okay" } catch { case okay: Exception => }
15+
16+
try { "okay" } catch { case _ if "".isEmpty => }
17+
18+
"okay" match { case _ => "" }
19+
}

0 commit comments

Comments
 (0)