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

@JsonCodec produces warnings on generic case class with -Wunused:params #1411

Closed
oleg-py opened this issue Mar 2, 2020 · 7 comments
Closed

Comments

@oleg-py
Copy link

oleg-py commented Mar 2, 2020

circe 0.13.0
Annotating a generic case class with @JsonCodec:

@io.circe.generic.JsonCodec
case class Foo[A](data: A)
object Test extends App

Produces warnings:

parameter value encode0 in method encodeFoo is never used
parameter value decode0 in method decodeFoo is never used

Scastie: https://scastie.scala-lang.org/bGI7rDGURBWynQNaajI6vg

@travisbrown
Copy link
Member

This is a bug in the compiler, which you can reproduce without the macro annotation:

import io.circe.Decoder
import io.circe.generic.semiauto.deriveDecoder

case class Foo[A](data: A)

object Foo {
  implicit def decodeFoo[A](implicit decode0: Decoder[A]): Decoder[Foo[A]] =
    deriveDecoder[Foo[A]]
}

…which gives:

// Exiting paste mode, now interpreting.

         implicit def decodeFoo[A](implicit decode0: Decoder[A]): Decoder[Foo[A]] =
                                            ^
On line 7: warning: parameter value decode0 in method decodeFoo is never used

The decode0 instance is very much not unused.

If someone knows a workaround here, I'd be happy to hear about it, but I hate these macro annotations and am looking forward to the day they'll be gone, so I'm inclined to close this as out-of-scope for Circe.

Thanks for the report, though, @oleg-py—I've seen questions about this before, but I don't think we had a proper issue.

@oleg-py
Copy link
Author

oleg-py commented Mar 2, 2020

The common thing to do with unused implicits is just

object Foo {
  implicit def decodeFoo[A](implicit decode0: Decoder[A]): Decoder[Foo[A]] = {
    val _ = decode0
    deriveDecoder[Foo[A]]
  }
}

thought that won't scale with number of type parameters. You can also have a helper unused function instead:

object Foo {
  implicit def decodeFoo[A](implicit decode0: Decoder[A]): Decoder[Foo[A]] = {
    io.circe.util.unused(decode0)
    deriveDecoder[Foo[A]]
  }
}

That of course has the drawback of polluting the library.

The third way I know of - not sure if it's supported by current Scala macros, but I've used it in better-monadic-for - is to mark methods and/or implicit parameters as synthetic and/or artifact (I don't know the difference except that they both work to silence many warnings).

@nafg
Copy link

nafg commented Mar 2, 2021

I only started getting this now on scala 2.13.5. Did something change?

@oleg-py
Copy link
Author

oleg-py commented Mar 2, 2021

2.13.5 has added linting for unused context bounds (this pull).

@GlassAndOneHalf
Copy link

The same seems to happen with deriveEncoder & deriveDecoder from the semiauto package. For now I have suppressed the warnings using @nowarn.

@hamnis
Copy link
Collaborator

hamnis commented Jul 27, 2023

This is a compiler issue, not circe.

@hamnis hamnis closed this as completed Jul 27, 2023
@hvesalai
Copy link

Did anybody report this to the scala developers?

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

6 participants