Skip to content
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

Allow empty if statements if they're captured by a function #720

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -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")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't have my IDE here, but I think the original authors' intent of scapegoat was to write:

Future("test").toOption match {
  case Some("foo") => ()
  case _ => throw new IllegalStateException("Bar!")
}

Does it make sense?

Copy link
Contributor Author

@Johnnei Johnnei Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely sure I follow, I based the sample on a further simplified version of the one in the issue:

  def checkFoo(things: Future[Seq[String]]): Future[Unit] = {
    things.map(ts => if (ts.contains("foo")) () else throw new Exception("No foo!"))
  }

The comment with the pattern matching is a valid workaround for this warning (although I'd prefer the transformWith solution which doesn't use flow control mechanisms), but does it really solve the problem scapegoat highlights? Instead of an empty if branch you now have an empty case branch. While empty case branches are maybe a bit more common to see, I wouldn't really see it as much different.
So I tend to agree with the the original issue and consider it a false-positive for cases where the empty branch (if or case in that regard) are being captured by the return type.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it really solve the problem scapegoat highlights

I think the intended thinking was that empty branches in ifs are not OK, but empty case is fine.
Nevertheless, I don't feel strongly about that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if @sksamuel speaks up soon, I'd let him decide.
Otherwise I am fine with merging that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no strong opinion either so we can merge.

| ()
| } else {
| throw new IllegalStateException("Bar!")
| }
| })
|}""".stripMargin

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