Skip to content

Suppress spurious Suppression #22383

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

Merged
merged 1 commit into from
Jan 20, 2025
Merged

Conversation

som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Jan 15, 2025

Guard against multiple registrations of nowarn on related elements.

Fixes #18341

@som-snytt
Copy link
Contributor Author

som-snytt commented Jan 16, 2025

By making it a case class so toString is useful, I can tell that not all the suppressions were the same. Here it's visible that end is different in the second suppression, so discarding it is bad.

ADD Suppression(tests/warn/i18341.scala:<296..303>,List(Any),296,296,false)

ADD Suppression(tests/warn/i18341.scala:<296..303>,List(Any),296,446,false)

In fact the duplication is because it sticks to everything:

  class B(@nowarn() useless: Int) extends Object() {
    @nowarn() private[this] val useless: Int
  }
  class C(@nowarn("msg=unused") useless: Int) extends Object() {
    @nowarn("msg=unused") private[this] val useless: Int
  }
  class D(useless: Int) extends Object() {
    private[this] val useless: Int
  }
  class E(@nowarn() useful: Int) extends Object() {
    @nowarn() private[this] val useful: Int
    def e: Int = E.this.useful.*(10)
  }
  class X() extends Object() {
    def extensionInCompanion: String = ???
  }
  @nowarn() final lazy module val X: X = new X()
  @nowarn() final module class X() extends Object() { this: X.type =>
    implicit def companionConversion(x: X): B = ???
    extension (x: X) def extensionInCompanion: String = ???
  }

Does this make the annotation useless? Well, check unused does say that a param is used iff its param accessor is used. Both should warn or neither; similarly for modules in whatever you call their dual nature. Normally, the "duplicate" warning would be ignored at the same position, assuming the message text also aligns.

But that is nonsense. The sensible way is to ignore duplicate nowarns with equal (start, end) and also of zero extent.

@som-snytt som-snytt marked this pull request as ready for review January 16, 2025 01:21
@som-snytt som-snytt changed the title Add Suppression only once Suppress spurious Suppression Jan 16, 2025
Copy link
Member

@KacperFKorban KacperFKorban left a comment

Choose a reason for hiding this comment

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

Looks Good! Thanks!

@KacperFKorban KacperFKorban merged commit a377c8a into scala:main Jan 20, 2025
29 checks passed
@som-snytt som-snytt deleted the issue/18341 branch January 20, 2025 11:01
@WojciechMazur WojciechMazur added this to the 3.7.0 milestone Mar 11, 2025
tgodzik pushed a commit to scala/scala3-lts that referenced this pull request Apr 1, 2025
Guard against multiple registrations of nowarn on related elements.

Fixes scala#18341
tgodzik added a commit to scala/scala3-lts that referenced this pull request Apr 1, 2025
Guard against multiple registrations of nowarn on related elements.

Fixes scala#18341
[Cherry-picked a377c8a][modified]
Gedochao pushed a commit that referenced this pull request Aug 6, 2025
Fixes #23651 

The [previous fix](#22383) for the
same `@nowarn` attached to multiple elements should have compared the
`annotPos` to identify duplicates (instead of the target range).

This commit defers detecting "bad" or duplicate suppressions (which
originate with the same annotation) to report time, after the
suppression is "unused"; there are few nowarns per file and fewer that
are unused.

While checking for a suppression, mark matching unused suppressions as
"superseded", so that if they remain unused, the warning can add an
"audit" that the nowarn matched a diagnostic (but was superseded by some
other nowarn).

~This commit goes further and checks for duplicates (including whether
the filters look the same).~

~If it finds a duplicate where the `annotPos` differs, warn about the
user-written annotation.~

~Filters match each other if they are the same type and, if they have a
pattern, the string representations of the patterns are equal.~
tgodzik pushed a commit to scala/scala3-lts that referenced this pull request Aug 8, 2025
Fixes scala#23651

The [previous fix](scala#22383) for the
same `@nowarn` attached to multiple elements should have compared the
`annotPos` to identify duplicates (instead of the target range).

This commit defers detecting "bad" or duplicate suppressions (which
originate with the same annotation) to report time, after the
suppression is "unused"; there are few nowarns per file and fewer that
are unused.

While checking for a suppression, mark matching unused suppressions as
"superseded", so that if they remain unused, the warning can add an
"audit" that the nowarn matched a diagnostic (but was superseded by some
other nowarn).

~This commit goes further and checks for duplicates (including whether
the filters look the same).~

~If it finds a duplicate where the `annotPos` differs, warn about the
user-written annotation.~

~Filters match each other if they are the same type and, if they have a
pattern, the string representations of the patterns are equal.~
tgodzik added a commit to scala/scala3-lts that referenced this pull request Aug 8, 2025
Fixes scala#23651 

The [previous fix](scala#22383) for the
same `@nowarn` attached to multiple elements should have compared the
`annotPos` to identify duplicates (instead of the target range).

This commit defers detecting "bad" or duplicate suppressions (which
originate with the same annotation) to report time, after the
suppression is "unused"; there are few nowarns per file and fewer that
are unused.

While checking for a suppression, mark matching unused suppressions as
"superseded", so that if they remain unused, the warning can add an
"audit" that the nowarn matched a diagnostic (but was superseded by some
other nowarn).

~This commit goes further and checks for duplicates (including whether
the filters look the same).~

~If it finds a duplicate where the `annotPos` differs, warn about the
user-written annotation.~

~Filters match each other if they are the same type and, if they have a
pattern, the string representations of the patterns are equal.~
[Cherry-picked 40843f7][modified]
WojciechMazur pushed a commit that referenced this pull request Aug 11, 2025
Fixes #23651 

The [previous fix](#22383) for the
same `@nowarn` attached to multiple elements should have compared the
`annotPos` to identify duplicates (instead of the target range).

This commit defers detecting "bad" or duplicate suppressions (which
originate with the same annotation) to report time, after the
suppression is "unused"; there are few nowarns per file and fewer that
are unused.

While checking for a suppression, mark matching unused suppressions as
"superseded", so that if they remain unused, the warning can add an
"audit" that the nowarn matched a diagnostic (but was superseded by some
other nowarn).

~This commit goes further and checks for duplicates (including whether
the filters look the same).~

~If it finds a duplicate where the `annotPos` differs, warn about the
user-written annotation.~

~Filters match each other if they are the same type and, if they have a
pattern, the string representations of the patterns are equal.~
[Cherry-picked 40843f7]
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.

-Wunused:nowarn falsely reports that an annotation does not suppress any warnings
3 participants