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

[help-wanted] ForbidInference behaves differently for Scala 2 and 3 #905

Open
danicheg opened this issue Aug 11, 2023 · 0 comments
Open

Comments

@danicheg
Copy link

  • Scala version: 2.13.11 and 3.3.0
  • wartremover version: 3.1.3 (more precise on the current master)

Hey there. I found that org.wartremover.warts.ForbidInference behaves differently for Scala 2 and 3 with such nested types as Option[Option[_]]:

// Scala 2
object NestedOption extends ForbidInference[Option[Option[_]]] {
  def apply(u: WartUniverse): u.Traverser = applyForbidden(u)
}

// Scala 3
object NestedOption extends ForbidInference[Option[Option[?]]]

// tests
class NestedOptionTest extends AnyFunSuite with ResultAssertions {
  test("Option[_] can be inferred") {
    val result = WartTestTraverser(NestedOption) {
      Option(42)
    }
    assertEmpty(result)
  }

  test("Option[List[_]] can be inferred") {
    val result = WartTestTraverser(NestedOption) {
      Option(List(42))
    }
    assertEmpty(result)
  }

  test("Option[Option[_]] can't be inferred") {
    val result = WartTestTraverser(NestedOption) {
      Option(Option(42))
    }
    assertError(result)("Inferred type containing Option: Option[Int]")
  }

  test("Option[Option[Option[_]]] can't be inferred") {
    val result = WartTestTraverser(NestedOption) {
      Option(Option(None))
    }
    assertError(result)("Inferred type containing Option: Option[None.type]")
  }

  test("NestedOption wart obeys SuppressWarnings") {
    val result = WartTestTraverser(NestedOption) {
      @SuppressWarnings(Array("org.wartremover.warts.NestedOption"))
      val x = Option(Option(42))
    }
    assertEmpty(result)
  }
}

Using Scala 2, all tests are passed, but for using Scala 3, only NestedOption wart obeys SuppressWarnings is passed. I might miss something stupid and obvious, so feel free to point me at that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant