Skip to content

Commit

Permalink
Enable -Xlint
Browse files Browse the repository at this point in the history
Linting on 2.13, but difficult to align 3.x.

Can't use unused annotation 2.12.
  • Loading branch information
som-snytt committed Apr 10, 2024
1 parent d8a5b13 commit e6f3dff
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 26 deletions.
50 changes: 41 additions & 9 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def scala213 = "2.13.13"

def scala212 = "2.12.19"

def scala3 = "3.1.2"
def scala3 = "3.3.3"
def junitVersion = "4.13.2"
def gcp = "com.google.cloud" % "google-cloud-storage" % "2.32.1"
inThisBuild(
Expand Down Expand Up @@ -154,7 +154,16 @@ val sharedJVMSettings: List[Def.Setting[_]] = List(

val sharedJSSettings: List[Def.Setting[_]] = List(
skipIdeaSettings,
crossScalaVersions := allScalaVersions.filterNot(_.startsWith("0."))
crossScalaVersions := allScalaVersions.filterNot(_.startsWith("0.")),
scalacOptions ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, _)) =>
List(
"-P:scalajs:nowarnGlobalExecutionContext",
)
case _ => Nil
}
}
)
val sharedJSConfigure: Project => Project =
_.disablePlugins(MimaPlugin)
Expand All @@ -176,17 +185,40 @@ val sharedSettings = List(
"-Ywarn-unused-import",
"-target:jvm-1.8"
)
case Some((major, _)) if major != 2 =>
case Some((2, 12)) =>
List(
"-language:implicitConversions"
"-feature",
"-target:jvm-1.8",
"-Ywarn-unused-import",
"-Yrangepos"
)
case Some((2, _)) =>
List(
"-deprecation", // enable deprecation first because it sets -Wconf:cat=deprecation:w
"-feature",
"--release:8",
"-unchecked",
"-Wconf:cat=deprecation&origin=scala.collection.JavaConverters:s",
"-Wconf:cat=deprecation&origin=scala.reflect.api.Trees.New:s", // no quasiquotes
"-Wconf:cat=deprecation&origin=scala.reflect.api.Position.lineContent:s", // use it anyway
"-Wconf:msg=parameter:s", // quash other messages about unused parameters
"-Wunused:params",
//"-Wconf:cat=unused-privates&msg=never used:s",
//"-Wconf:cat=unused-params&msg=never used:s",
//"-Werror",
"-Xlint:-deprecation,_",
"-Yrangepos", // default
)
case _ =>
List(
"-target:jvm-1.8",
"-Yrangepos",
// -Xlint is unusable because of
// https://github.com/scala/bug/issues/10448
"-Ywarn-unused:imports"
"-deprecation",
"-feature",
"-java-output-version:8",
"-Wconf:cat=deprecation&msg=Converters:s",
"-Wconf:cat=deprecation&msg=New:s", // no quasiquotes
"-Wconf:cat=deprecation&msg=Position:s",
//"-Wunused:all",
//"-language:implicitConversions"
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def check[T](

check("basic", List(1, 2), Some(1))
check("empty", List(), Some(1))
check("null", List(null, 2), Some(null))
check("null", List(null, "more"), Some(null))
```

When declaring tests in a helper function, it's useful to pass around an
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package munit

import language.implicitConversions

import org.scalacheck.Prop
import org.scalacheck.{Test => ScalaCheckTest}
import org.scalacheck.util.Pretty
import org.scalacheck.rng.Seed
import scala.annotation.nowarn
import scala.util.Success
import scala.util.Failure
import scala.util.Try
Expand All @@ -25,6 +28,7 @@ trait ScalaCheckSuite extends FunSuite {
// Allow property bodies of type Unit
// This is done to support using MUnit assertions in property bodies
// instead of returning a Boolean.
@nowarn("msg=used")
implicit def unitToProp(unit: Unit): Prop = Prop.passed

override def munitTestTransforms: List[TestTransform] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package munit.internal.junitinterface
import munit.internal.console.AnsiColors
import sbt.testing._
import munit.internal.PlatformCompat
import scala.annotation.nowarn

final class JUnitReporter(
eventHandler: EventHandler,
Expand Down Expand Up @@ -35,6 +36,7 @@ final class JUnitReporter(
}
emitEvent(method, Status.Ignored)
}
@nowarn("msg=used")
def reportAssumptionViolation(
method: String,
timeInSeconds: Double,
Expand Down Expand Up @@ -219,6 +221,7 @@ final class JUnitReporter(
.map(_.getFileName)
.orNull

@nowarn("msg=used")
private def stackTraceElementToString(
e: StackTraceElement,
testFileName: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package munit.internal.junitinterface

import sbt.testing._
import munit.internal.PlatformCompat
import scala.annotation.nowarn

@nowarn("msg=used")
final class JUnitRunner(
val args: Array[String],
_remoteArgs: Array[String],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.junit.experimental.categories

import scala.annotation.Annotation
import scala.annotation.nowarn

@nowarn("msg=used")
class Category(classes: Array[Class[_]]) extends Annotation
2 changes: 2 additions & 0 deletions munit/js-native/src/main/scala/org/junit/runner/RunWith.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.junit.runner

import scala.annotation.Annotation
import scala.annotation.nowarn

@nowarn("msg=used")
class RunWith(cls: Class[_]) extends Annotation
6 changes: 4 additions & 2 deletions munit/js/src/main/scala/munit/internal/PlatformCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package munit.internal
import scala.scalajs.reflect.Reflect
import sbt.testing.TaskDef
import munit.MUnitRunner
import scala.concurrent.Future
import sbt.testing.Task
import sbt.testing.EventHandler
import sbt.testing.Logger
import scala.annotation.nowarn
import scala.concurrent.Future
import scala.concurrent.Promise
import scala.concurrent.duration.Duration
import scala.concurrent.Await
Expand Down Expand Up @@ -36,7 +37,7 @@ object PlatformCompat {
ec: ExecutionContext
): Future[T] = {
val onComplete = Promise[T]()
val timeoutHandle = timers.setTimeout(duration.toMillis) {
val timeoutHandle = timers.setTimeout(duration.toMillis.toDouble) {
onComplete.tryFailure(
new TimeoutException(s"test timed out after $duration")
)
Expand Down Expand Up @@ -65,6 +66,7 @@ object PlatformCompat {
def isJS: Boolean = true
def isNative: Boolean = false

@nowarn("msg=used")
def newRunner(
taskDef: TaskDef,
classLoader: ClassLoader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import scala.scalanative.reflect.Reflect
import sbt.testing.Task
import sbt.testing.EventHandler
import sbt.testing.Logger
import scala.annotation.nowarn
import scala.concurrent.Await
import scala.concurrent.Awaitable
import scala.concurrent.duration.Duration
Expand All @@ -26,6 +27,7 @@ object PlatformCompat {
task.execute(eventHandler, loggers)
Future.successful(())
}
@nowarn("msg=used")
def waitAtMost[T](
startFuture: () => Future[T],
duration: Duration,
Expand All @@ -47,6 +49,7 @@ object PlatformCompat {
def isJS: Boolean = false
def isNative: Boolean = true

@nowarn("msg=used")
def newRunner(
taskDef: TaskDef,
classLoader: ClassLoader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package munit.internal.junitinterface
import munit.internal.PlatformCompat
import org.junit.runner.notification.RunNotifier
import sbt.testing._
import scala.concurrent.ExecutionContext.Implicits.global

/* Implementation note: In JUnitTask we use Future[Try[Unit]] instead of simply
* Future[Unit]. This is to prevent Scala's Future implementation to box/wrap
Expand Down
2 changes: 2 additions & 0 deletions munit/shared/src/main/scala-2.13/munit/internal/Compat.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package munit.internal

import language.reflectiveCalls

object Compat {
type LazyList[+T] = scala.LazyList[T]
val LazyList = scala.LazyList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package munit.internal

import munit.Clue
import munit.Location
import scala.language.implicitConversions
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import munit.Clue
import munit.Location
import scala.quoted._
import scala.language.experimental.macros
import scala.language.implicitConversions

object MacroCompat {

Expand All @@ -15,7 +16,7 @@ object MacroCompat {
def locationImpl()(using Quotes): Expr[Location] = {
import quotes.reflect._
val pos = Position.ofMacroExpansion
val path = pos.sourceFile.jpath.toString
val path = pos.sourceFile.path
val startLine = pos.startLine + 1
'{ new Location(${ Expr(path) }, ${ Expr(startLine) }) }
}
Expand Down
7 changes: 4 additions & 3 deletions munit/shared/src/main/scala/munit/Assertions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import munit.internal.console.{Lines, Printers, StackTraces}
import munit.internal.difflib.ComparisonFailExceptionHandler
import munit.internal.difflib.Diffs

import scala.annotation.nowarn
import scala.collection.mutable
import scala.reflect.ClassTag
import scala.util.control.NonFatal
import scala.collection.mutable
import munit.internal.console.AnsiColors
import org.junit.AssumptionViolatedException
import munit.internal.MacroCompat
Expand Down Expand Up @@ -37,7 +38,7 @@ trait Assertions extends MacroCompat.CompileErrorMacro {
def assume(
cond: Boolean,
clue: => Any = "assumption failed"
)(implicit loc: Location): Unit = {
)(implicit @nowarn loc: Location): Unit = {
StackTraces.dropInside {
if (!cond) {
throw new AssumptionViolatedException(munitPrint(clue))
Expand Down Expand Up @@ -95,7 +96,7 @@ trait Assertions extends MacroCompat.CompileErrorMacro {
StackTraces.dropInside {
if (!compare.isEqual(obtained, expected)) {
(obtained, expected) match {
case (a: Array[_], b: Array[_]) if a.sameElements(b) =>
case (a: Array[_], b: Array[_]) if a.sameElements[Any](b) =>
// Special-case error message when comparing arrays. See
// https://github.com/scalameta/munit/pull/393 and
// https://github.com/scalameta/munit/issues/339 for a related
Expand Down
7 changes: 4 additions & 3 deletions munit/shared/src/main/scala/munit/MUnitRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.junit.runner.notification.Failure
import org.junit.runner.notification.RunNotifier

import java.lang.reflect.Modifier
import scala.annotation.nowarn
import scala.collection.mutable
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
Expand All @@ -29,7 +30,7 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
with Configurable {

def this(cls: Class[_ <: Suite]) =
this(MUnitRunner.ensureEligibleConstructor(cls), () => cls.newInstance())
this(MUnitRunner.ensureEligibleConstructor(cls), () => cls.getDeclaredConstructor().newInstance())

val suite: Suite = newInstance()

Expand Down Expand Up @@ -92,7 +93,7 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
val desc = Description.createTestDescription(
cls,
testName,
test.annotations: _*
test.annotations.toIndexedSeq: _*
)
desc
}
Expand Down Expand Up @@ -168,6 +169,7 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
loop(futures, mutable.ListBuffer.empty)
}

@nowarn
private def munitTimeout(): Option[Duration] = {
suite match {
case funSuite: FunSuite => Some(funSuite.munitTimeout)
Expand Down Expand Up @@ -245,7 +247,6 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
test: Test
): Future[BeforeAllResult] = {
val context = new BeforeEach(test)
val fixtures = mutable.ListBuffer.empty[AnyFixture[_]]
sequenceFutures(
munitFixtures.iterator.map(f =>
valueTransform(() => f.beforeEach(context)).map(_ => f)
Expand Down
2 changes: 2 additions & 0 deletions munit/shared/src/main/scala/munit/TestOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ object TestOptions extends TestOptionsConversions {

trait TestOptionsConversions {

import language.implicitConversions

/**
* Implicitly create a TestOptions given a test name.
* This allows writing `test("name") { ... }` even if `test` accepts a `TestOptions`
Expand Down
2 changes: 1 addition & 1 deletion munit/shared/src/main/scala/munit/ValueTransforms.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait ValueTransforms { this: BaseFunSuite =>
val nested: Future[Future[Any]] = future.map { value =>
val transformed = munitValueTransforms.iterator
.map(fn => fn(value))
.collectFirst { case Some(future) => future }
.collectFirst { case Some(f) => f }
transformed match {
case Some(f) => flattenFuture(f)
case None => Future.successful(value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package munit.internal

import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.util.Try
import scala.concurrent.ExecutionContext

object FutureCompat {
implicit class ExtensionFuture[T](f: Future[T]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object Diffs {
def create(obtained: String, expected: String): Diff =
new Diff(obtained, expected)

@deprecated("")
@deprecated("", since="")
def assertNoDiff(
obtained: String,
expected: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait Equalizer[T] {
object Equalizer {
def default[T]: Equalizer[T] = new Equalizer[T] {
override def equals(original: T, revised: T): Boolean = {
original.equals(revised)
original == revised //original.equals(revised)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scala.concurrent.Future
import scala.concurrent.Promise

class AsyncFunFixtureOrderSuite extends FunSuite {
val latch: Promise[Unit] = Promise[Unit]
val latch: Promise[Unit] = Promise[Unit]()
var completedFromTest: Option[Boolean] = None
var completedFromTeardown: Option[Boolean] = None

Expand Down
2 changes: 1 addition & 1 deletion tests/shared/src/test/scala/munit/PrintersSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class PrintersSuite extends FunSuite { self =>

check(
"list",
List(1, 2, 3, List(4, 5, List(6, 7))),
List[Any](1, 2, 3, List[Any](4, 5, List(6, 7))),
"""|List(
| 1,
| 2,
Expand Down

0 comments on commit e6f3dff

Please sign in to comment.