Skip to content

Commit a3315cd

Browse files
committed
SI-2807 Avoid catch all warning for Stable Id patterns
1 parent cd1e6f9 commit a3315cd

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4865,9 +4865,10 @@ trait Typers extends Modes with Adaptations with Tags {
48654865

48664866
for (cdef <- catches1 if cdef.guard.isEmpty) {
48674867
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.")
4868+
def unbound(t: Tree) = t.symbol == null || t.symbol == NoSymbol
48684869
cdef.pat match {
4869-
case Bind(name, Ident(_)) => warn(name)
4870-
case Ident(name) => warn(name)
4870+
case Bind(name, i@Ident(_)) if unbound(i) => warn(name)
4871+
case i@Ident(name) if unbound(i) => warn(name)
48714872
case _ =>
48724873
}
48734874
}

test/files/neg/catch-all.check

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
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-
^
2+
try { "warn" } catch { case _ => }
3+
^
44
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-
^
5+
try { "warn" } catch { case x => }
6+
^
77
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-
^
8+
try { "warn" } catch { case _: RuntimeException => ; case x => }
9+
^
1010
three errors found

test/files/neg/catch-all.scala

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
object CatchAll {
2-
try { "warn" } catch { case _ => }
2+
try { "warn" } catch { case _ => }
33

4-
try { "warn" } catch { case x => }
4+
try { "warn" } catch { case x => }
55

6-
try { "warn" } catch { case _: RuntimeException => ; case x => }
6+
try { "warn" } catch { case _: RuntimeException => ; case x => }
77

8-
try { "okay" } catch { case _: Throwable => }
8+
val t = T
99

10-
try { "okay" } catch { case _: Exception => }
10+
try { "okay" } catch { case T => }
1111

12-
try { "okay" } catch { case okay: Throwable => }
12+
try { "okay" } catch { case `t` => }
1313

14-
try { "okay" } catch { case okay: Exception => }
14+
try { "okay" } catch { case x @ T => }
1515

16-
try { "okay" } catch { case _ if "".isEmpty => }
16+
try { "okay" } catch { case x @ `t` => }
1717

18-
"okay" match { case _ => "" }
18+
try { "okay" } catch { case _: Throwable => }
19+
20+
try { "okay" } catch { case _: Exception => }
21+
22+
try { "okay" } catch { case okay: Throwable => }
23+
24+
try { "okay" } catch { case okay: Exception => }
25+
26+
try { "okay" } catch { case _ if "".isEmpty => }
27+
28+
"okay" match { case _ => "" }
1929
}
30+
31+
object T extends Throwable

0 commit comments

Comments
 (0)