Skip to content

add NothingAsFunctionArgument rule to the Analyzer Plugin #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
d8f04ff
add ThrownExceptionNotInMonixScope rule to the Analyzer Plugin
halotukozak Jun 12, 2024
8e4fa2e
change to all function parameters
halotukozak Jun 14, 2024
8aeed88
add some `flatMap` cases
halotukozak Jun 18, 2024
9622a3f
Nothing is a special bottom type; it is a subtype of every other type…
halotukozak Jun 19, 2024
4b554f2
Revert "Nothing is a special bottom type; it is a subtype of every ot…
halotukozak Jun 19, 2024
4f836b1
handle multiple arguments, not only the first one
halotukozak Jun 19, 2024
e259d63
add more tests
halotukozak Jun 19, 2024
ae2b07a
add more tests
halotukozak Jun 19, 2024
da13377
add and fix test
halotukozak Jun 24, 2024
0c3b54c
lil enhancement
halotukozak Jun 24, 2024
0a59f61
Merge branch 'refs/heads/master' into 500-handle-thrown-exception-out…
halotukozak Jul 19, 2024
3fea6b1
Merge branch 'master' into 500-handle-thrown-exception-outside-the-mo…
halotukozak Jun 20, 2025
4a7b996
better error message
halotukozak Jun 20, 2025
9f38ad9
Merge remote-tracking branch 'origin/master' into 500-handle-thrown-e…
halotukozak Jun 25, 2025
a515f6f
Refactor `ThrownExceptionNotInFunctionTest` using `AnyWordSpec` and `…
halotukozak Jun 25, 2025
64c9db9
Enhance `ThrownExceptionNotInFunctionTest` for clarity and accuracy i…
halotukozak Jun 25, 2025
370e4fd
renaming + docs
halotukozak Jun 25, 2025
6222088
Rename `ThrowAsFunctionArgument` to `NothingAsFunctionArgument`, upda…
halotukozak Jun 25, 2025
f9e48dd
Merge remote-tracking branch 'origin/master' into 500-handle-thrown-e…
halotukozak Jun 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ final class AnalyzerPlugin(val global: Global) extends Plugin { plugin =>
new Any2StringAdd(global),
new ThrowableObjects(global),
new DiscardedMonixTask(global),
new ThrownExceptionNotInMonixScope(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,59 @@
package com.avsystem.commons
package analyzer

import scala.tools.nsc.Global

final class ThrownExceptionNotInMonixScope(g: Global) extends AnalyzerRule(g, "thrownExceptionNotInMonixScope") {

import global.*

lazy val monixTaskTpe: Type = classType("monix.eval.Task") match {
case NoType => NoType
case tpe => TypeRef(NoPrefix, tpe.typeSymbol, List(definitions.AnyTpe))
}
private def checkDiscardedNothing(tree: Tree, discarded: Boolean): Unit = tree match {
case tree if !discarded && tree.tpe != null && tree.tpe =:= definitions.NothingTpe =>
checkDiscardedNothing(tree, discarded = true)

case Block(stats: List[Tree], expr: Tree) =>
stats.foreach(checkDiscardedNothing(_, discarded = true))
checkDiscardedNothing(expr, discarded)

case Template(parents: List[Tree], self: ValDef, body: List[Tree]) =>
parents.foreach(checkDiscardedNothing(_, discarded = false))
checkDiscardedNothing(self, discarded = false)
body.foreach(checkDiscardedNothing(_, discarded = true))

case If(_: Tree, thenp: Tree, elsep: Tree) =>
checkDiscardedNothing(thenp, discarded)
checkDiscardedNothing(elsep, discarded)

case LabelDef(_: TermName, _: List[Ident], rhs: Tree) =>
checkDiscardedNothing(rhs, discarded = true)

case Try(block: Tree, catches: List[CaseDef], finalizer: Tree) =>
checkDiscardedNothing(block, discarded)
catches.foreach(checkDiscardedNothing(_, discarded))
checkDiscardedNothing(finalizer, discarded = true)

case CaseDef(_: Tree, _: Tree, body: Tree) =>
checkDiscardedNothing(body, discarded)

case Match(_: Tree, cases: List[CaseDef]) =>
cases.foreach(checkDiscardedNothing(_, discarded))

case Annotated(_: Tree, arg: Tree) =>
checkDiscardedNothing(arg, discarded)

case Typed(expr: Tree, _: Tree) =>
checkDiscardedNothing(expr, discarded)

case Apply(TypeApply(Select(prefix: Tree, TermName("map")), List(_)), List(Throw(_))) if prefix.tpe <:< monixTaskTpe =>
report(tree.pos, "exception thrown not in Monix Task scope ")

case tree =>
tree.children.foreach(checkDiscardedNothing(_, discarded = false))
}

def analyze(unit: CompilationUnit): Unit = checkDiscardedNothing(unit.body, discarded = false)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.avsystem.commons
package analyzer

import org.scalatest.funsuite.AnyFunSuite

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

test("simple") {
assertErrors(10,
//language=Scala
"""
|import monix.eval.Task
|
|object whatever {
| def task: Task[String] = ???
| def ex: Exception = ???
|
| // errors from these
| task.map(throw ex)
|
| {
| println(""); task.map(throw ex)
| }
|
| if (true) task.map(throw ex) else task.map(throw ex)
|
| try task.map(throw ex) catch {
| case _: Exception => task.map(throw ex)
| } finally task.map(throw ex)
|
| Seq(1, 2, 3).foreach(_ => task.map(throw ex))
|
| while (true) task.map(throw ex)
|
| do task.map(throw ex) while (true)
|
| // no errors from these
| task.map(_ => throw ex)
|
| {
| println(""); task.map(_ => throw ex)
| }
|
| if (true) task.map(_ => throw ex) else task.map(_ => throw ex)
|
| try task.map(_ => throw ex) catch {
| case _: Exception => task.map(_ => throw ex)
| } finally task.map(_ => throw ex)
|
| Seq(1, 2, 3).foreach(_ => task.map(_ => throw ex))
|
| while (true) task.map(_ => throw ex)
|
| do task.map(_ => throw ex) while (true)
|}
|
|""".stripMargin
)
}
}