Skip to content

Commit 587bbf4

Browse files
committed
Post-process invalid or duplicate suppressions
1 parent 0992048 commit 587bbf4

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

compiler/src/dotty/tools/dotc/Run.scala

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,11 @@ extends ImplicitRunInfo, ConstraintRunInfo, cc.CaptureRunInfo {
118118
.merge
119119
addSuppression:
120120
Suppression(annotPos, filters, range.start, range.end, verbose)
121-
.tap: sup =>
122-
if filters == List(MessageFilter.None) then sup.markUsed() // invalid suppressions, don't report as unused
123121

124122
def addSuppression(sup: Suppression): Unit =
125123
val suppressions = mySuppressions.getOrElseUpdate(sup.annotPos.source, ListBuffer.empty)
126124
if sup.start != sup.end then
127-
suppressions.find(sup.matches(_)) match
128-
case Some(other) =>
129-
if sup.annotPos != other.annotPos then
130-
report.warning("@nowarn annotation is duplicate", sup.annotPos)
131-
case none =>
132-
suppressions += sup
125+
suppressions += sup
133126

134127
def reportSuspendedMessages(source: SourceFile)(using Context): Unit = {
135128
// sort suppressions. they are not added in any particular order because of lazy type completion
@@ -147,10 +140,14 @@ extends ImplicitRunInfo, ConstraintRunInfo, cc.CaptureRunInfo {
147140
for
148141
source <- mySuppressions.keysIterator.toList
149142
sups <- mySuppressions.remove(source)
150-
sup <- sups.reverse
151-
if !sup.used
152143
do
153-
report.warning("@nowarn annotation does not suppress any warnings", sup.annotPos)
144+
val suppressions = sups.reverse.toList
145+
for sup <- suppressions do
146+
if !sup.used
147+
&& !suppressions.exists(s => s.ne(sup) && s.used && s.annotPos == sup.annotPos) // duplicate
148+
&& sup.filters != List(MessageFilter.None) // invalid suppression, don't report as unused
149+
then
150+
report.warning("@nowarn annotation does not suppress any warnings", sup.annotPos)
154151

155152
/** The compilation units currently being compiled, this may return different
156153
* results over time.

tests/warn/i23651.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ class A
99
class B
1010

1111
@nowarn("msg=class A is deprecated")
12-
@nowarn("cat=deprecation&msg=class A is deprecated") // warn unused redundant
12+
@nowarn("cat=deprecation&msg=class A is deprecated") // warn @nowarn annotation does not suppress any warnings
1313
@nowarn("cat=deprecation&msg=class B is deprecated")
1414
trait C1 {
1515
def a: A
1616
def b: B
1717
}
1818

1919
@nowarn("cat=deprecation&msg=class B is deprecated")
20-
@nowarn("cat=deprecation&msg=class B is deprecated") // warn duplicate
20+
@nowarn("cat=deprecation&msg=class B is deprecated") // warn @nowarn annotation does not suppress any warnings
2121
@nowarn("cat=deprecation&msg=class A is deprecated")
2222
trait C2 {
2323
def a: A

0 commit comments

Comments
 (0)