Skip to content

Commit

Permalink
change to all function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
halotukozak committed Jun 14, 2024
1 parent d8f04ff commit 8e4fa2e
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class AnalyzerPlugin(val global: Global) extends Plugin { plugin =>
new Any2StringAdd(global),
new ThrowableObjects(global),
new DiscardedMonixTask(global),
new ThrownExceptionNotInMonixScope(global),
new ThrownExceptionNotInFunction(global),
new BadSingletonComponent(global),
new ConstantDeclarations(global),
new BasePackage(global),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.avsystem.commons
package analyzer

import scala.tools.nsc.Global

final class ThrownExceptionNotInFunction(g: Global) extends AnalyzerRule(g, "thrownExceptionNotInFunction") {

import global.*

def analyze(unit: CompilationUnit): Unit = unit.body.foreach(analyzeTree {
case t@Apply(f: TypeApply, List(Throw(_))) if definitions.isFunctionType(f.tpe.params.head.tpe) =>
report(t.pos, "exception thrown in place of function definition")
})
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.avsystem.commons
package analyzer

import org.scalatest.funsuite.AnyFunSuite

final class ThrownExceptionNotInFunctionTest extends AnyFunSuite with AnalyzerTest {
settings.pluginOptions.value ++= List("AVSystemAnalyzer:-discardedMonixTask")

Seq(
("Option[_]", "map"),
("List[_]", "map"),
("Seq[_]", "map"),
("Set[_]", "map"),
("Map[_, _]", "map"),
("scala.concurrent.Future[_]", "map"),
("scala.util.Try[_]", "map"),
("Either[_, _]", "map"),
("monix.eval.Task[_]", "map"),
("com.avsystem.commons.misc.Opt[_]", "map"),
("String => Int", "andThen"),
("String => Nothing", "andThen"),
("Nothing => Nothing", "andThen"),
("String => Int", "compose"),
("Seq[_]", "foreach"),
).foreach { case (tpe, function) =>
test(s"Testing $function of $tpe") {
assertErrors(10,
//language=Scala
s"""
|object whatever {
| implicit val ec: scala.concurrent.ExecutionContext = ??? // for Future
|
| def sth: $tpe = ???
| def ex: Exception = ???
|
| // errors from these
| sth.$function(throw ex)
|
| {
| println(""); sth.$function(throw ex)
| }
|
| if (true) sth.$function(throw ex) else sth.$function(throw ex)
|
| try sth.$function(throw ex) catch {
| case _: Exception => sth.$function(throw ex)
| } finally sth.$function(throw ex)
|
| Seq(1, 2, 3).foreach(_ => sth.$function(throw ex))
|
| while (true) sth.$function(throw ex)
|
| do sth.$function(throw ex) while (true)
|
| // no errors from these
| sth.$function(_ => throw ex)
|
| {
| println(""); sth.$function(_ => throw ex)
| }
|
| if (true) sth.$function(_ => throw ex) else sth.$function(_ => throw ex)
|
| try sth.$function(_ => throw ex) catch {
| case _: Exception => sth.$function(_ => throw ex)
| } finally sth.$function(_ => throw ex)
|
| Seq(1, 2, 3).foreach(_ => sth.$function(_ => throw ex))
|
| while (true) sth.$function(_ => throw ex)
|
| do sth.$function(_ => throw ex) while (true)
|}
|
|""".stripMargin
)
}
}
}

This file was deleted.

0 comments on commit 8e4fa2e

Please sign in to comment.