Skip to content

Fix false unused import warning for given CanEqual in pattern matching#25231

Open
Bbn08 wants to merge 1 commit intoscala:mainfrom
Bbn08:fix/unused-given-import-pattern-match
Open

Fix false unused import warning for given CanEqual in pattern matching#25231
Bbn08 wants to merge 1 commit intoscala:mainfrom
Bbn08:fix/unused-given-import-pattern-match

Conversation

@Bbn08
Copy link
Contributor

@Bbn08 Bbn08 commented Feb 10, 2026

Problem

Fixes #25227

Under strict equality, import Givens.given (providing CanEqual[Any, Null]) is falsely reported as unused when the given is consumed only by pattern matching (case null =>), even though removing it breaks compilation. The warning does not appear when the given is consumed by ==/!=.

import scala.language.strictEquality

object Givens:
  given CanEqual[Any, Null] = CanEqual.derived

object Test:
  import Givens.given  // ← falsely reported as unused

  def m(value: Any): Boolean = value match
    case null => true   // needs CanEqual[Any, Null]
    case _ => false

Root Cause

CheckUnused.transformApply correctly resolves CanEqual for ==/!= via resolveScoped(caneq), marking the given import as used. However, transformMatch had no equivalent logic for pattern matching against literal/null patterns, so the import was never marked as used.

Fix

Added the same resolveScoped(caneq) logic to transformMatch for Literal patterns (including null). When a Match tree has a literal pattern, we construct CanEqual[SelectorType, PatternType] and call resolveScoped to mark any given import providing it as used.

Testing

New test tests/warn/i25227.scala with both positive (used by case null =>) and negative (genuinely unused) cases
All existing warn tests pass, including unused-can-equal.scala and i17762.scala

@som-snytt
Copy link
Contributor

There is a PR #25114 to move the lint after patter matcher to detect these comparisons.

@Bbn08
Copy link
Contributor Author

Bbn08 commented Feb 10, 2026

@som-snytt Thanks for pointing to #25114, I believe your fix should address this as well, since moving CheckUnused to run after pattern matching desugaring means literal/null patterns will already be lowered to equality checks that the existing transformApply logic handles. That's a much more elegant and comprehensive approach than what I had.

I had put together a smaller, targeted fix that adds resolveScoped(caneq) directly in transformMatch for literal patterns, which could potentially serve as a complementary defense-in-depth measure.

That said, if your merged fix already covers this case fully, I'm happy to close my PR please let me know. Thanks for the great work on this!

Copy link
Contributor

@som-snytt som-snytt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed it is fixed by the other PR, which is merged, but it would be nice to have this test without the code change!

@Bbn08 Bbn08 force-pushed the fix/unused-given-import-pattern-match branch from e238a24 to 296a0ba Compare February 19, 2026 09:18
@Bbn08
Copy link
Contributor Author

Bbn08 commented Feb 19, 2026

I've updated this PR to be test only the code changes to CheckUnused have been removed, since #25114 (now merged) addresses the underlying issue by moving the lint phase after the pattern matcher.

What this PR now contains:

  • A single new file tests/warn/i25227.scala, a regression test for False unused import warnings when importing "given" #25227
  • The test verifies that -Wunused:all correctly reports unused given CanEqual imports under strictEquality, including the case where a given import is consumed only by case null => pattern matching

I'm sorry if this change took more than the expected time had something going on in life. Thank you.

@Bbn08 Bbn08 force-pushed the fix/unused-given-import-pattern-match branch from dabde04 to 079bf8c Compare February 19, 2026 18:01
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

Successfully merging this pull request may close these issues.

False unused import warnings when importing "given"

3 participants

Comments