Skip to content

Commit

Permalink
Allow unit results in if statement if they're captured by a function (#…
Browse files Browse the repository at this point in the history
…720)

Fixes #229
  • Loading branch information
Johnnei authored Jan 23, 2023
1 parent cdc8606 commit 6ac7fb0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class EmptyIfBlock

override def inspect(tree: Tree): Unit = {
tree match {
// Function directly contains if, an empty block would generate Unit (or Any) output type
case Function(_, If(_, Literal(Constant(())), _)) => // skip tree
case If(_, Literal(Constant(())), _) =>
context.warn(tree.pos, self, tree.toString.take(500))
case _ => continue(tree)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class EmptyIfBlockTest extends InspectionTest {
"empty if block" - {
"should report warning" in {

val code = """object Test {
val code =
"""object Test {
if (true) {
}
Expand All @@ -28,5 +29,26 @@ class EmptyIfBlockTest extends InspectionTest {
compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 2
}

"should not report warning" - {
"the if-block is the return value" in {
val code =
"""
|import scala.concurrent.Future
|import scala.concurrent.ExecutionContext.Implicits.global
|object Test {
| Future("test").map(value => {
| if (value.contains("foo")) {
| ()
| } else {
| throw new IllegalStateException("Bar!")
| }
| })
|}""".stripMargin

compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 0
}
}
}
}

0 comments on commit 6ac7fb0

Please sign in to comment.