Skip to content

Commit

Permalink
Add inspection for "ZIO.fail(throw" (#491)
Browse files Browse the repository at this point in the history
* Add inspection for "ZIO.fail(throw"

* Add inspection for "ZIO.fail(throw"

* Add inspection for "ZIO.fail(throw"
  • Loading branch information
andrzejressel authored Nov 23, 2024
1 parent 13b4fd2 commit 857c7f9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@
shortName="IncorrectPreludeNewTypeAliasInspection" level="ERROR"
enabledByDefault="true" language="Scala"/>

<localInspection implementationClass="zio.intellij.inspections.mistakes.ZIOFailThrowInspection"
displayName="Detects erroneous throw inside `ZIO.fail` instead of returning exception value"
groupPath="Scala,ZIO" groupName="Inspections"
shortName="ZIOFailThrowInspection" level="WARNING"
enabledByDefault="true" language="Scala"/>

<intentionAction>
<category>ZIO/Suggestions</category>
<className>zio.intellij.intentions.suggestions.SuggestTypeAlias</className>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package zio.intellij.inspections.mistakes

import com.intellij.codeInspection.{LocalInspectionTool, ProblemHighlightType, ProblemsHolder}
import com.intellij.openapi.project.Project
import org.jetbrains.plugins.scala.codeInspection.{AbstractFixOnPsiElement, PsiElementVisitorSimple}
import org.jetbrains.plugins.scala.lang.psi.api.expr.{ScExpression, ScThrow}
import zio.intellij.inspections.`ZIO.fail`

class ZIOFailThrowInspection extends LocalInspectionTool {

override def buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitorSimple = {
case fail @ `ZIO.fail`(_, scThrow: ScThrow) =>
scThrow.expression match {
case Some(throwableExpr) =>
holder.registerProblem(
fail,
ZIOFailThrowInspection.message,
ProblemHighlightType.WARNING,
new QuickFix(scThrow, throwableExpr)
)
case None =>
}
case _ =>
}

final class QuickFix(
val toReplace: ScExpression,
val replaceWith: ScExpression
) extends AbstractFixOnPsiElement(s"Remove throw from ZIO.fail", toReplace) {

override protected def doApplyFix(element: ScExpression)(implicit project: Project): Unit =
element.replace(replaceWith)
}

}

object ZIOFailThrowInspection {
val message = "Mistaken throwing of the exception inside ZIO.fail"
}
22 changes: 22 additions & 0 deletions src/test/scala/zio/inspections/ZIOFailThrowInspectionTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package zio.inspections

import zio.intellij.inspections.mistakes.ZIOFailThrowInspection

class ZIOFailThrowInspectionTest extends ZScalaInspectionTest[ZIOFailThrowInspection] {

override protected def description: String = ZIOFailThrowInspection.message
val hint = "Remove throw from ZIO.fail"

def test_throw_inside_zio_fail(): Unit = {
z(s"""for {
| x <- ${START}ZIO.fail(throw new RuntimeException("FAIL"))${END}
|} yield ()""".stripMargin).assertHighlighted()
val text = z(s"""for {
| x <- ZIO.fail(throw new RuntimeException("FAIL"))
|} yield ()""".stripMargin)
val result = z(s"""for {
| x <- ZIO.fail(new RuntimeException("FAIL"))
|} yield ()""".stripMargin)
testQuickFix(text, result, hint)
}
}

0 comments on commit 857c7f9

Please sign in to comment.