Skip to content

False positive unused given in extensions #23125

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

Open
WojciechMazur opened this issue May 8, 2025 · 4 comments · May be fixed by #23132
Open

False positive unused given in extensions #23125

WojciechMazur opened this issue May 8, 2025 · 4 comments · May be fixed by #23132
Assignees
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug regression This worked in a previous version but doesn't anymore stat:fixed in nightly This issue may be present in the latest stable or RC version of Next, but has been since fixed.

Comments

@WojciechMazur
Copy link
Contributor

WojciechMazur commented May 8, 2025

Compiler version

3.7.0
3.7.1-RC1

Fixed in 3.7.2-RC1-NIGHTLY

Bisect points it was fixed in 32defdf

Minimized code

//> using options -Wunused:all 

trait MonadThrow[F[_]]
extension [T](value: T) def liftTo[F[_]](using MonadThrow[F]): F[T] = ???

trait Api[F[_]]:
  def setMaxAge(value: Int): F[Unit]
  def truncate(): F[Unit]

trait MetaStreamsSyntax:
  extension [F[_]](ms: Api[F])(using MonadThrow[F]) // warns: 
    def withMaxAge(): F[Unit] = 
      val lifted = 0.liftTo[F] // uses MonadThrow
      assert(lifted != null) // to enusre this val is used
      assert(ms != null) // to ensure ms is used
      ???
      
    def truncated(): F[Unit] = 
      ms.truncate() // required to trigger warning, does not warn if ms is not used

Output

[warn] ./example.scala:11:38
[warn] unused implicit parameter
[warn]   extension [F[_]](ms: Api[F])(using MonadThrow[F])
[warn]            

Expectation

Should not warn. Bisected fix should be backported to 3.7.1-RC2 and regression test should be added

@WojciechMazur WojciechMazur added itype:bug regression This worked in a previous version but doesn't anymore area:linting Linting warnings enabled with -W or -Xlint stat:fixed in nightly This issue may be present in the latest stable or RC version of Next, but has been since fixed. labels May 8, 2025
@som-snytt
Copy link
Contributor

I'll take a second look, as it should not warn in either location (for any type).

@xuwei-k
Copy link
Contributor

xuwei-k commented May 9, 2025

https://github.com/sbt/sbt/blob/84ebcd3c34b09464333a8ad1ef9696b2494b9a30/tasks-standard/src/main/scala/sbt/std/TaskExtra.scala#L208-L216

Maybe same issue in sbt 2.x with Scala 3.7.0

[warn] -- [E198] Unused Symbol Warning: sbt/tasks-standard/src/main/scala/sbt/std/TaskExtra.scala:208:10 
[warn] 208 |  )(using streams: Task[TaskStreams[Key]], key: Task[?] => Key) {
[warn]     |          ^^^^^^^
[warn]     |          unused implicit parameter
[warn] -- [E198] Unused Symbol Warning: sbt/tasks-standard/src/main/scala/sbt/std/TaskExtra.scala:208:43 
[warn] 208 |  )(using streams: Task[TaskStreams[Key]], key: Task[?] => Key) {
[warn]     |                                           ^^^
[warn]     |                                           unused implicit parameter

@som-snytt
Copy link
Contributor

On first glance, I missed that there is a second extension method in the "collective".

That is really just a second method that takes the "shared" parameter, and it is unused there.

This works as expected. The latest lint will not warn about a "marker" trait, however, so MonadThrow must have a member to demonstrate the warning.

@som-snytt
Copy link
Contributor

som-snytt commented May 9, 2025

I'll submit a PR so that the message looks like

-- [E198] Unused Symbol Warning: tests/warn/i23125.scala:13:37 -----------------
13 |  extension [F[_]](ms: Api[F])(using MonadThrow[F]) // no warns:
   |                                     ^
   |                 unused implicit parameter in extension method truncated
1 warning found

That is for any extension method; my first idea was to add words when the param is on a different line from the owner method, but I was lazy.

#23132

@som-snytt som-snytt linked a pull request May 9, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug regression This worked in a previous version but doesn't anymore stat:fixed in nightly This issue may be present in the latest stable or RC version of Next, but has been since fixed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants