Skip to content

Commit e1451b0

Browse files
committed
improve handling of custom handler CaseDefs in CatchThrowable
1 parent 0893569 commit e1451b0

File tree

2 files changed

+59
-54
lines changed

2 files changed

+59
-54
lines changed

analyzer/src/main/scala/com/avsystem/commons/analyzer/CatchThrowable.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ class CatchThrowable(g: Global) extends AnalyzerRule(g, "catchThrowable", Level.
2424
t.catches.foreach {
2525
case CaseDef(Alternative(trees), _, _) => trees.foreach(checkTree)
2626
case CaseDef(Bind(_, Alternative(trees)), _, _) => trees.foreach(checkTree)
27-
case CaseDef(pat, _, _) => checkTree(pat)
27+
// CaseDef generated from a custom handler has NoPosition
28+
case cd@CaseDef(pat, _, _) if cd.pos != NoPosition => checkTree(pat)
29+
case _ =>
2830
}
2931
case _ =>
3032
}

analyzer/src/test/scala/com/avsystem/commons/analyzer/CatchThrowableTest.scala

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,47 @@ final class CatchThrowableTest extends AnyFunSuite with AnalyzerTest {
77
test("catching Throwable should be rejected") {
88
assertErrors(1,
99
scala"""
10-
|def test(): Unit =
11-
| try {
12-
| println("test")
13-
| } catch {
14-
| case t: Throwable => println(t)
15-
| }
10+
|try {
11+
| println("test")
12+
|} catch {
13+
| case t: Throwable => println(t)
14+
|}
1615
|""".stripMargin)
1716
}
1817

1918
test("catching specific exceptions should be allowed") {
2019
assertNoErrors(
2120
scala"""
22-
|def test(): Unit =
23-
| try {
24-
| println("test")
25-
| } catch {
26-
| case e: Exception => println(e)
27-
| case e: RuntimeException => println(e)
28-
| case e: IllegalArgumentException => println(e)
29-
| }
21+
|try {
22+
| println("test")
23+
|} catch {
24+
| case e: Exception => println(e)
25+
| case e: RuntimeException => println(e)
26+
| case e: IllegalArgumentException => println(e)
27+
|}
3028
|""".stripMargin)
3129
}
3230

3331
test("catching Throwable with other exceptions should be rejected") {
3432
assertErrors(1,
3533
scala"""
36-
|def test(): Unit = {
37-
| try {
38-
| println("test")
39-
| } catch {
40-
| case e: IllegalArgumentException => println(e)
41-
| case t: Throwable => println(t)
42-
| }
34+
|try {
35+
| println("test")
36+
|} catch {
37+
| case e: IllegalArgumentException => println(e)
38+
| case t: Throwable => println(t)
4339
|}
4440
|""".stripMargin)
4541
}
4642

4743
test("catching Throwable in nested catch block should be rejected") {
4844
assertErrors(1,
4945
scala"""
50-
|def test(): Unit = {
51-
| try println("test")
46+
|try println("test")
47+
|catch {
48+
| case e: Exception => try println("test")
5249
| catch {
53-
| case e: Exception => try println("test")
54-
| catch {
55-
| case e: Throwable => println(e)
56-
| }
50+
| case e: Throwable => println(e)
5751
| }
5852
|}
5953
|""".stripMargin)
@@ -70,46 +64,55 @@ final class CatchThrowableTest extends AnyFunSuite with AnalyzerTest {
7064
| case _ => None
7165
| }
7266
|}
73-
|def test(): Unit = {
74-
| try {
75-
| println("test")
76-
| } catch {
77-
| case custom(t) => println(t)
78-
| case NonFatal(t) => println(t)
79-
| case scala.util.control.NonFatal(t) => println(t)
80-
| }
67+
|
68+
|try {
69+
| println("test")
70+
|} catch {
71+
| case custom(t) => println(t)
72+
| case NonFatal(t) => println(t)
73+
| case scala.util.control.NonFatal(t) => println(t)
8174
|}
8275
|""".stripMargin)
8376
}
8477

8578
test("catching non-Throwable with pattern match should be allowed") {
8679
assertNoErrors(
8780
scala"""
88-
|def test(): Unit = {
89-
| try {
90-
| println("test")
91-
| } catch {
92-
| case _: IndexOutOfBoundsException | _: NullPointerException => println("OK!")
93-
| }
94-
| try {
95-
| println("test")
96-
| } catch {
97-
| case e@(_: IndexOutOfBoundsException | _: NullPointerException) => println("OK!")
98-
| }
81+
|try {
82+
| println("test")
83+
|} catch {
84+
| case _: IndexOutOfBoundsException | _: NullPointerException => println("OK!")
9985
|}
100-
|""".stripMargin)
86+
|try {
87+
| println("test")
88+
|} catch {
89+
| case e@(_: IndexOutOfBoundsException | _: NullPointerException) => println("OK!")
90+
|}
91+
|""".stripMargin
92+
)
10193
}
10294

10395
test("catching Throwable with pattern match should be rejected") {
10496
assertErrors(1,
10597
scala"""
106-
|def test(): Unit = {
107-
| try {
108-
| println("test")
109-
| } catch {
110-
| case _: IndexOutOfBoundsException | _: Throwable => println("Not OK!")
111-
| }
98+
|try {
99+
| println("test")
100+
|} catch {
101+
| case _: IndexOutOfBoundsException | _: Throwable => println("Not OK!")
112102
|}
113103
|""".stripMargin)
114104
}
105+
106+
test("catching Throwable using custom handler should be allowed") {
107+
assertNoErrors(
108+
scala"""
109+
|object CustomHandler {
110+
| def apply[T](): PartialFunction[Throwable, T] = ???
111+
|}
112+
|
113+
|try {
114+
| println("test")
115+
|} catch CustomHandler()
116+
|""".stripMargin)
117+
}
115118
}

0 commit comments

Comments
 (0)