How do I apply @nowarn("msg=unused import") to an import in Scala 3?

Hi all,

On Scala 3.3.7 with -Wunused:imports, I’m getting a false-positive “unused import” on a given wildcard import that the code needs. Removing it breaks the compile.

The library declares the given inside a library-private trait that’s mixed into a public object. I can’t name the specific given for a targeted import (the declaring trait is private), so I need the whole given. An operator defined elsewhere in the library resolves implicitly to that given. The unused-imports checker doesn’t trace implicit resolution through the mixin chain and flags the import as unused.

I’d like to suppress this one warning with @nowarn. Three forms I tried, none worked:

  1. @nowarn(“msg=unused import”) import api.given → parse error: Expected start of definition
  2. @nowarn on the enclosing case class → warning still fires (imports are file-level)
  3. Move import api.given inside the class body, @nowarn on it → same parse error

Is there a supported syntax to annotate an import with @nowarn in 3.3.x? Short of a file-scoped -Wconf:src=…&cat=unused-imports:s in the build, is there an idiomatic way to quiet this?

Thanks,

David

import scala.annotation.nowarn

import calico.html.io.{button, cls, div, onClick, styleAttr, given}
import cats.effect.{IO, Resource}
import fs2.concurrent.SignallingRef
import fs2.dom.HtmlElement

class Widget:
  def render: Resource[IO, HtmlElement[IO]] =
    for
      show <- SignallingRef[IO].of(false).toResource
      popup <- div(
        styleAttr <-- show.map(s => if s then "display: block" else "display: none"),
        button(
          onClick --> (_.foreach(_ => show.set(false))),
          "Done"
        )
      )
      widget <- div(
        cls := "ctrl",
        popup
      )
    yield widget

// Attempts that do NOT suppress the warning on Scala 3.3.7:
//
//   @nowarn("msg=unused import") import calico.html.io.given
//       → parse error: "Expected start of definition"
//
//   @nowarn("msg=unused import")
//   class Widget: ...               // ignored — imports are file-level
//
//   class Widget:
//     @nowarn("msg=unused import")
//     import calico.html.io.given   // parse error
2 Likes

Because imports cannot be annotated, -Wconf:origin=some.Thing:s will silence that unused import message.

I see from the unit test for that feature (because I forgot) that -Wconf:any:verbose will tell you what to do.

If you update your example with scala-cli directives using dep and so on, I’ll take a look at why you’re getting a false positive. Sometimes 3.8 is more effective than LTS.

2 Likes

3.8.3 fixes it. Thanks!