-
Notifications
You must be signed in to change notification settings - Fork 23
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
halotukozak
merged 19 commits into
master
from
500-handle-thrown-exception-outside-the-monadic-context-of-task
Jun 26, 2025
Merged
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 8e4fa2e
change to all function parameters
halotukozak 8aeed88
add some `flatMap` cases
halotukozak 9622a3f
Nothing is a special bottom type; it is a subtype of every other type…
halotukozak 4b554f2
Revert "Nothing is a special bottom type; it is a subtype of every ot…
halotukozak 4f836b1
handle multiple arguments, not only the first one
halotukozak e259d63
add more tests
halotukozak ae2b07a
add more tests
halotukozak da13377
add and fix test
halotukozak 0c3b54c
lil enhancement
halotukozak 0a59f61
Merge branch 'refs/heads/master' into 500-handle-thrown-exception-out…
halotukozak 3fea6b1
Merge branch 'master' into 500-handle-thrown-exception-outside-the-mo…
halotukozak 4a7b996
better error message
halotukozak 9f38ad9
Merge remote-tracking branch 'origin/master' into 500-handle-thrown-e…
halotukozak a515f6f
Refactor `ThrownExceptionNotInFunctionTest` using `AnyWordSpec` and `…
halotukozak 64c9db9
Enhance `ThrownExceptionNotInFunctionTest` for clarity and accuracy i…
halotukozak 370e4fd
renaming + docs
halotukozak 6222088
Rename `ThrowAsFunctionArgument` to `NothingAsFunctionArgument`, upda…
halotukozak f9e48dd
Merge remote-tracking branch 'origin/master' into 500-handle-thrown-e…
halotukozak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
analyzer/src/main/scala/com/avsystem/commons/analyzer/ThrownExceptionNotInMonixScope.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
halotukozak marked this conversation as resolved.
Show resolved
Hide resolved
|
61 changes: 61 additions & 0 deletions
61
...zer/src/test/scala/com/avsystem/commons/analyzer/ThrownExceptionNotInMonixScopeTest.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.