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

.to(Col) vs "unused import" warning #240

Closed
Daenyth opened this issue Jul 30, 2019 · 14 comments
Closed

.to(Col) vs "unused import" warning #240

Daenyth opened this issue Jul 30, 2019 · 14 comments

Comments

@Daenyth
Copy link
Contributor

Daenyth commented Jul 30, 2019

I'm not sure how fixable this is, but using .to(Coll) on 2.13 causes "unused import" warnings, which is a pain when you have fatal-warnings enabled.

example: https://scastie.scala-lang.org/mc1Z3irYQ72gmPNgI96c6g

As a workaround right now I'm disabling the 2.13 version of the flag: scalacOptions -= "-Wunused:imports"

@SethTisue
Copy link
Member

@som-snytt there's a recent ticket on this elsewhere, isn't there?

@som-snytt
Copy link

I'll check it out. Unused imports usually just works.

@som-snytt
Copy link

OK, I see what you mean. They recommend that you use the "empty" import in 2.13, but of course it is actually unused.

What a conundrum.

Worst case, the warning could special-case the compatibility package under 2.13.

Alternatively, don't warn if an import clause imports nothing. (That is less appealing.)

@som-snytt
Copy link

I don't know if this is too quick and dirty, but scala/scala#8308

@NthPortal
Copy link
Contributor

alternate alternative idea: separate warning flag -Wunused:empty-imports (-Wunused:imports.empty?) which is set by -Wunused:imports by default, but can be separately disabled, so you can do -Wunused:imports -Wunused:imports.empty:false (is that the correct syntax?)

@som-snytt
Copy link

Unfortunately, the import is not empty. Either the source preprocessor or the reporter postprocessor is required. Alternatively, a scalafix that is run as a source generator.

@SethTisue
Copy link
Member

is this -Wconf/@nowarnable?

@valencik
Copy link

valencik commented Jan 30, 2021

I don't think @nowarn works on imports, at least I couldn't get scalac to like any of my attempts.

-Wconf seems more promising but I am not sure how to match exactly the warning we have.
Consider:

sbt:http4s> core / compile
[info] compiling 219 Scala sources to /home/andrew/grabbed/http4s/core/target/scala-2.13/classes ...
[warn] /home/andrew/grabbed/http4s/core/src/main/scala/org/http4s/Query.scala:33:32: Unused import
[warn] import scala.collection.compat._
[warn]                                ^
[warn] one warning found

Running set scalacOptions in core ++= Seq("-Wconf:cat=unused-imports:silent") in sbt followed by core / compile does work, but this is too much, we're ignoring all unused imports.

Sadly set scalacOptions in core ++= Seq("-Wconf:msg=import scala\.collection\.compat\._:silent") does not work, because our warning message is actually just Unused import, it doesn't include any of the other output.

Update: I don't think we can use origin or site filters either.

origin is just for deprecations.
And site turns out to be the package for imports. Consider:

package scala.collection {
  package object compat
}

package here.is.a.test {
  import scala.collection.compat._
}

If we compile this with scalacOptions "-Wunused", "-Wconf:cat=unused-imports:wv"
(I'm doing this in sbt and using: set scalacOptions in ThisBuild := Seq("-Wunused", "-Wconf:cat=unused-imports:wv"))
We get:

[info] compiling 1 Scala source to /home/andrew/src/scala-unused-import/target/scala-2.13/classes ...
[warn] /home/andrew/src/scala-unused-import/unused.scala:6:34: [unused-imports @ here.is.a.test] Unused import
[warn]   import scala.collection.compat._
[warn]                                  ^
[warn] one warning found

You can see the site value is here.is.a.test which won't help us ignore unused import warnings on scala.collection.compat._ in general.

Compiler code constructing the warning message here.

@som-snytt
Copy link

FSR no one mentioned -Yimports, which manipulates the root contexts (not really imports) and is not subject to unused warnings. -Yimports:java.lang,scala,scala.Predef,scala.collection.compat.

I guess the reason is that the option is not available for 2.12. How very unfortunate!

If the import were needed only on 2.13, then it would work as expected.

At least I had an opportunity to re-read https://www.scala-sbt.org/1.x/docs/Cross-Build.html yet again but I forgot to notify Guinness book of world records before I attempted it. Maybe I can still get a mention in Ripley's Believe it or not.

@som-snytt
Copy link

I spent some time with -Wconf:help, and I think site is workable, though it may be ungainly.

crossScalaVersions := Seq("2.12.12", "2.13.4")

libraryDependencies += "org.scala-lang.modules" %% "scala-collection-compat" % "2.4.1"

// gratuitous, as the import is required for compiling under 2.12
Compile / scalacOptions ++= {
  CrossVersion.partialVersion(scalaVersion.value) match {
    case Some((2, n)) if n <= 12 => Seq("-Xlint")
    case _                       => Seq("-Xlint", raw"-Wconf:cat=unused-imports&site=p\.(Main|C):s,any:wv")
    //case _                       => Seq("-Xlint", "-Yimports:java.lang,scala,scala.Predef,scala.collection.compat")
  }
}

Compile / scalacOptions ++= Seq("-Xfatal-warnings")

with source file

package p

object Main extends App {
  import scala.collection.compat._
  val xs = Seq("a","b","c")
  println(xs.to(List))
}

class C {
  import scala.collection.compat._
  val xs = Seq("a","b","c")
  def f = xs.to(List)
}

There is a trade-off between specificity of import and complexity of the -Wconf:site pattern.

Issue for origin:

scala/bug#12326

@som-snytt
Copy link

One way to ensure your package is used on import:

    package object compat {
      implicit def $conforms[A]: A => A = <:<.refl
    }

@som-snytt
Copy link

The linked ticket was fixed. I am empowered to close this ticket, so I will do so in that context.

@SethTisue
Copy link
Member

SethTisue commented Nov 14, 2024

to save others some digging: in Scala 2, scala/scala#9939 added support for origin= filtering on unused imports

whether Scala 3 also currently offers it, I don't know

@som-snytt
Copy link

origin is scala/scala3#21404 and I'll check that imports uses it at scala/scala3#20894

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

No branches or pull requests

5 participants