Skip to content

Commit

Permalink
PredefTraversableIsMutable & PredefIterableIsMutable don't apply on S…
Browse files Browse the repository at this point in the history
…cala 2.13 (#708)
  • Loading branch information
saeltz authored Nov 26, 2022
1 parent 98cfc7e commit 4bb1be2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sksamuel.scapegoat.inspections.collections

import com.sksamuel.scapegoat.{Inspection, InspectionContext, Inspector, Levels}
import com.sksamuel.scapegoat.{isScala213, Inspection, InspectionContext, Inspector, Levels}

/**
* @author
Expand All @@ -15,6 +15,8 @@ class PredefIterableIsMutable
"Iterable aliases scala.collection.mutable.Iterable. Did you intend to use an immutable Iterable?"
) {

override def isEnabled: Boolean = !isScala213

def inspector(context: InspectionContext): Inspector =
new Inspector(context) {
override def postTyperTraverser: context.Traverser =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sksamuel.scapegoat.inspections.collections

import com.sksamuel.scapegoat.{Inspection, InspectionContext, Inspector, Levels}
import com.sksamuel.scapegoat.{isScala213, Inspection, InspectionContext, Inspector, Levels}

/**
* @author
Expand All @@ -15,6 +15,8 @@ class PredefTraversableIsMutable
"Traversable aliases scala.collection.mutable.Traversable. Did you intend to use an immutable Traversable?"
) {

override def isEnabled: Boolean = !isScala213

def inspector(context: InspectionContext): Inspector =
new Inspector(context) {
override def postTyperTraverser: context.Traverser =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
package com.sksamuel.scapegoat.inspections.collections

import com.sksamuel.scapegoat.InspectionTest
import com.sksamuel.scapegoat.{isScala213, Inspection, InspectionTest}

/** @author Stephen Samuel */
class PredefIterableIsMutableTest extends InspectionTest {

override val inspections = Seq(new PredefIterableIsMutable)
override val inspections: Seq[Inspection] = Seq(new PredefIterableIsMutable)

"PredefIterableIsMutable" - {
"should report warning" - {
"for Iterable apply" in {
val code = """object Test { val a = Iterable("sammy") }""".stripMargin
val expectedWarnings = if (isScala213) 0 else 1
compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 1
compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings
}

"for declaring Iterable as return type" in {
val code = """object Test { def foo : Iterable[String] = ??? }""".stripMargin
val expectedWarnings = if (isScala213) 0 else 1
compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 1
compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings
}
}

"should not report warning" - {
"for scala.collection.mutable usage" in {
val code = """import scala.collection.mutable.Iterable
|object Test { val a = Iterable("sammy") }""".stripMargin
compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 0
}

"for scala.collection.immutable usage" in {
val code = """import scala.collection.immutable.Iterable
|object Test { val a = Iterable("sammy") }""".stripMargin
compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 0
}

"for scala.collection.mutable defs" in {
val code = """import scala.collection.mutable.Iterable
|object Test { def foo : Iterable[String] = ??? }""".stripMargin
compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 0
}

"for scala.collection.immutable defs" in {
val code = """import scala.collection.immutable.Iterable
|object Test { def foo : Iterable[String] = ??? }""".stripMargin
Expand Down

0 comments on commit 4bb1be2

Please sign in to comment.