Skip to content

Commit 8e4fa2e

Browse files
committed
change to all function parameters
1 parent d8f04ff commit 8e4fa2e

File tree

5 files changed

+95
-121
lines changed

5 files changed

+95
-121
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ final class AnalyzerPlugin(val global: Global) extends Plugin { plugin =>
5656
new Any2StringAdd(global),
5757
new ThrowableObjects(global),
5858
new DiscardedMonixTask(global),
59-
new ThrownExceptionNotInMonixScope(global),
59+
new ThrownExceptionNotInFunction(global),
6060
new BadSingletonComponent(global),
6161
new ConstantDeclarations(global),
6262
new BasePackage(global),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.avsystem.commons
2+
package analyzer
3+
4+
import scala.tools.nsc.Global
5+
6+
final class ThrownExceptionNotInFunction(g: Global) extends AnalyzerRule(g, "thrownExceptionNotInFunction") {
7+
8+
import global.*
9+
10+
def analyze(unit: CompilationUnit): Unit = unit.body.foreach(analyzeTree {
11+
case t@Apply(f: TypeApply, List(Throw(_))) if definitions.isFunctionType(f.tpe.params.head.tpe) =>
12+
report(t.pos, "exception thrown in place of function definition")
13+
})
14+
}

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

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.avsystem.commons
2+
package analyzer
3+
4+
import org.scalatest.funsuite.AnyFunSuite
5+
6+
final class ThrownExceptionNotInFunctionTest extends AnyFunSuite with AnalyzerTest {
7+
settings.pluginOptions.value ++= List("AVSystemAnalyzer:-discardedMonixTask")
8+
9+
Seq(
10+
("Option[_]", "map"),
11+
("List[_]", "map"),
12+
("Seq[_]", "map"),
13+
("Set[_]", "map"),
14+
("Map[_, _]", "map"),
15+
("scala.concurrent.Future[_]", "map"),
16+
("scala.util.Try[_]", "map"),
17+
("Either[_, _]", "map"),
18+
("monix.eval.Task[_]", "map"),
19+
("com.avsystem.commons.misc.Opt[_]", "map"),
20+
("String => Int", "andThen"),
21+
("String => Nothing", "andThen"),
22+
("Nothing => Nothing", "andThen"),
23+
("String => Int", "compose"),
24+
("Seq[_]", "foreach"),
25+
).foreach { case (tpe, function) =>
26+
test(s"Testing $function of $tpe") {
27+
assertErrors(10,
28+
//language=Scala
29+
s"""
30+
|object whatever {
31+
| implicit val ec: scala.concurrent.ExecutionContext = ??? // for Future
32+
|
33+
| def sth: $tpe = ???
34+
| def ex: Exception = ???
35+
|
36+
| // errors from these
37+
| sth.$function(throw ex)
38+
|
39+
| {
40+
| println(""); sth.$function(throw ex)
41+
| }
42+
|
43+
| if (true) sth.$function(throw ex) else sth.$function(throw ex)
44+
|
45+
| try sth.$function(throw ex) catch {
46+
| case _: Exception => sth.$function(throw ex)
47+
| } finally sth.$function(throw ex)
48+
|
49+
| Seq(1, 2, 3).foreach(_ => sth.$function(throw ex))
50+
|
51+
| while (true) sth.$function(throw ex)
52+
|
53+
| do sth.$function(throw ex) while (true)
54+
|
55+
| // no errors from these
56+
| sth.$function(_ => throw ex)
57+
|
58+
| {
59+
| println(""); sth.$function(_ => throw ex)
60+
| }
61+
|
62+
| if (true) sth.$function(_ => throw ex) else sth.$function(_ => throw ex)
63+
|
64+
| try sth.$function(_ => throw ex) catch {
65+
| case _: Exception => sth.$function(_ => throw ex)
66+
| } finally sth.$function(_ => throw ex)
67+
|
68+
| Seq(1, 2, 3).foreach(_ => sth.$function(_ => throw ex))
69+
|
70+
| while (true) sth.$function(_ => throw ex)
71+
|
72+
| do sth.$function(_ => throw ex) while (true)
73+
|}
74+
|
75+
|""".stripMargin
76+
)
77+
}
78+
}
79+
}
80+

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

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)