diff --git a/src/main/scala/com/sksamuel/scapegoat/inspections/unneccesary/UnnecessaryConversion.scala b/src/main/scala/com/sksamuel/scapegoat/inspections/unneccesary/UnnecessaryConversion.scala index abd7bee5..d680a7fc 100644 --- a/src/main/scala/com/sksamuel/scapegoat/inspections/unneccesary/UnnecessaryConversion.scala +++ b/src/main/scala/com/sksamuel/scapegoat/inspections/unneccesary/UnnecessaryConversion.scala @@ -1,6 +1,6 @@ package com.sksamuel.scapegoat.inspections.unneccesary -import com.sksamuel.scapegoat.{isScala21312, Inspection, InspectionContext, Inspector, Levels} +import com.sksamuel.scapegoat.{isScala21312OrLater, Inspection, InspectionContext, Inspector, Levels} /** * @author @@ -15,7 +15,7 @@ class UnnecessaryConversion "Calling e.g. toString on a String or toList on a List is completely unnecessary and it's an equivalent to identity." ) { - override def isEnabled: Boolean = !isScala21312 + override def isEnabled: Boolean = !isScala21312OrLater def inspector(ctx: InspectionContext): Inspector = new Inspector(ctx) { override def postTyperTraverser: context.Traverser = new context.Traverser { diff --git a/src/main/scala/com/sksamuel/scapegoat/package.scala b/src/main/scala/com/sksamuel/scapegoat/package.scala index 19ac8849..bf02ce2e 100644 --- a/src/main/scala/com/sksamuel/scapegoat/package.scala +++ b/src/main/scala/com/sksamuel/scapegoat/package.scala @@ -1,9 +1,19 @@ package com.sksamuel +import scala.util.Try + package object scapegoat { - val scalaVersion: String = util.Properties.versionNumberString - private val shortScalaVersion = scalaVersion.split('.').dropRight(1).mkString(".") + private val scalaVersion: String = util.Properties.versionNumberString + private val (major, minor, patch) = extractComponents(scalaVersion) + + val isScala213: Boolean = major == 2 && minor == 13 + val isScala21312OrLater: Boolean = isScala213 && patch >= 12 - val isScala213: Boolean = shortScalaVersion == "2.13" - val isScala21312: Boolean = scalaVersion == "2.13.12" + private[scapegoat] def extractComponents(version: String) = { + def parseInt(s: String) = Try(s.toInt).getOrElse(0) + version.split('.').toList.map(parseInt) match { + case List(major, minor, patch) => (major, minor, patch) + case _ => (1, 0, 0) + } + } } diff --git a/src/test/scala/com/sksamuel/scapegoat/ScapegoatTest.scala b/src/test/scala/com/sksamuel/scapegoat/ScapegoatTest.scala new file mode 100644 index 00000000..bb7233df --- /dev/null +++ b/src/test/scala/com/sksamuel/scapegoat/ScapegoatTest.scala @@ -0,0 +1,14 @@ +package com.sksamuel.scapegoat + +import com.sksamuel.scapegoat._ +import org.scalatest.freespec.AnyFreeSpec +import org.scalatest.matchers.should.Matchers + +class ScapegoatTest extends AnyFreeSpec with Matchers { + + "ScalaVersionPattern" - { + "shoud extract components" in { + extractComponents("2.13.12") shouldBe (2, 13, 12) + } + } +} diff --git a/src/test/scala/com/sksamuel/scapegoat/inspections/unnecessary/UnnecessaryConversionTest.scala b/src/test/scala/com/sksamuel/scapegoat/inspections/unnecessary/UnnecessaryConversionTest.scala index c8180c13..37b9208c 100644 --- a/src/test/scala/com/sksamuel/scapegoat/inspections/unnecessary/UnnecessaryConversionTest.scala +++ b/src/test/scala/com/sksamuel/scapegoat/inspections/unnecessary/UnnecessaryConversionTest.scala @@ -1,7 +1,7 @@ package com.sksamuel.scapegoat.inspections.unnecessary import com.sksamuel.scapegoat.inspections.unneccesary.UnnecessaryConversion -import com.sksamuel.scapegoat.{isScala21312, Inspection, InspectionTest} +import com.sksamuel.scapegoat.{isScala21312OrLater, Inspection, InspectionTest} /** @author Stephen Samuel */ class UnnecessaryConversionTest extends InspectionTest { @@ -17,7 +17,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21312OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -28,7 +28,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21312OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -39,7 +39,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 2 + val expectedWarnings = if (isScala21312OrLater) 0 else 2 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -50,7 +50,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21312OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -61,7 +61,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21312OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -72,7 +72,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21312OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -83,7 +83,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21312OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } @@ -94,7 +94,7 @@ class UnnecessaryConversionTest extends InspectionTest { |}""".stripMargin compileCodeSnippet(code) - val expectedWarnings = if (isScala21312) 0 else 1 + val expectedWarnings = if (isScala21312OrLater) 0 else 1 compiler.scapegoat.feedback.warnings.size shouldBe expectedWarnings } }