8000 Show nowarn / Wconf filters for a warning with @nowarn("verbose") · scala/scala@28087b5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 28087b5

Browse files
committed
Show nowarn / Wconf filters for a warning with @nowarn("verbose")
As already implemented in Scala 3 ``` scala> @nowarn("v") def f = try 1 ^ warning: A try without a catch or finally is equivalent to putting its body in a block; no exceptions are handled. Applicable -Wconf / @nowarn filters for this warning: msg=<part of the message>, cat=other, site=f def f: Int ```
1 parent 371c870 commit 28087b5

File tree

5 files changed

+55
-16
lines changed

5 files changed

+55
-16
lines changed

src/compiler/scala/tools/nsc/Reporting.scala

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,13 @@ trait Reporting extends internal.Reporting { self: ast.Positions with Compilatio
146146
suspendedMessages.clear()
147147
}
148148

149-
private def isSuppressed(warning: Message): Boolean =
149+
private def nowarnAction(warning: Message): Action =
150150
suppressions.getOrElse(repSrc(warning.pos.source), Nil).find(_.matches(warning)) match {
151-
case Some(s) => s.markUsed(); true
152-
case _ => false
151+
case Some(s) =>
152+
s.markUsed()
153+
if (s.verbose) Action.WarningVerbose else Action.Silent
154+
case _ =>
155+
Action.Warning
153156
}
154157

155158
def clearSuppressionsComplete(sourceFile: SourceFile): Unit = suppressionsComplete -= repSrc(sourceFile)
@@ -164,7 +167,7 @@ trait Reporting extends internal.Reporting { self: ast.Positions with Compilatio
164167

165168
def runFinished(hasErrors: Boolean): Unit = {
166169
// report suspended messages (in case the run finished before typer)
167-
suspendedMessages.valuesIterator.foreach(_.foreach(issueWarning))
170+
suspendedMessages.valuesIterator.foreach(_.foreach(issueWarning(_)))
168171

169172
// report unused nowarns only if all all phases are done. scaladoc doesn't run all phases.
170173
if (!hasErrors && settings.warnUnusedNowarn && !settings.isScaladoc)
@@ -196,8 +199,8 @@ trait Reporting extends internal.Reporting { self: ast.Positions with Compilatio
196199
sm.getOrElseUpdate(category, mutable.LinkedHashMap.empty)
197200
}
198201

199-
private def issueWarning(warning: Message): Unit = {
200-
val action = wconf.action(warning)
202+
private def issueWarning(warning: Message, verbose: Boolean = false): Unit = {
203+
val action = if (verbose) Action.WarningVerbose else wconf.action(warning)
201204

202205
val quickfixed = {
203206
if (!skipRewriteAction(action) && registerTextEdit(warning)) s"[rewritten by -quickfix] ${warning.msg}"
@@ -240,9 +243,12 @@ trait Reporting extends internal.Reporting { self: ast.Positions with Compilatio
240243
def issueIfNotSuppressed(warning: Message): Unit =
241244
if (shouldSuspend(warning))
242245
suspendedMessages.getOrElseUpdate(repSrc(warning.pos.source), mutable.LinkedHashSet.empty) += warning
243-
else {
244-
if (!isSuppressed(warning))
246+
else nowarnAction(warning) match {
247+
case Action.Warning =>
245248
issueWarning(warning)
249+
case Action.WarningVerbose =>
250+
issueWarning(warning, verbose = true)
251+
case _ =>
246252
}
247253

248254
private def summarize(action: Action, category: WarningCategory): Unit = {
@@ -895,7 +901,7 @@ object Reporting {
895901
}
896902
}
897903

898-
case class Suppression(annotPos: Position, filters: List[MessageFilter], start: Int, end: Int, synthetic: Boolean = false) {
904+
case class Suppression(annotPos: Position, filters: List[MessageFilter], start: Int, end: Int, synthetic: Boolean = false, verbose: Boolean = false) {
899905
private[this] var _used = false
900906
def used: Boolean = _used
901907
def markUsed(): Unit = { _used = true }

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff li 8000 ne change
@@ -4059,19 +4059,24 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
40594059
}
40604060
def registerNowarn(info: AnnotationInfo): Unit = {
40614061
if (annotee.isDefined && NowarnClass.exists && info.matches(NowarnClass) && !runReporting.suppressionExists(info.pos)) {
4062+
var verbose = false
40624063
val filters = (info.assocs: @unchecked) match {
40634064
case Nil => List(MessageFilter.Any)
40644065
case (_, LiteralAnnotArg(s)) :: Nil =>
4065-
if (s.stringValue.isEmpty) Nil
4066-
else {
4067-
val (ms, fs) = s.stringValue.split('&').map(WConf.parseFilter(_, runReporting.rootDirPrefix)).toList.partitionMap(identity)
4066+
val str = s.stringValue
4067+
if (str.isEmpty) Nil
4068+
else if (str == "v" || str == "verbose") {
4069+
verbose = true
4070+
List(MessageFilter.Any)
4071+
} else {
4072+
val (ms, fs) = str.split('&').map(WConf.parseFilter(_, runReporting.rootDirPrefix)).toList.partitionMap(identity)
40684073
if (ms.nonEmpty)
40694074
reporter.error(info.pos, s"Invalid message filter:\n${ms.mkString("\n")}")
40704075
fs
40714076
8000 }
40724077
}
40734078
val (start, end) = rangeFinder()
4074-
runReporting.addSuppression(Suppression(info.pos, filters, start, end))
4079+
runReporting.addSuppression(Suppression(info.pos, filters, start, end, verbose = verbose))
40754080
}
40764081
}
40774082
def registerDeprecationSuppression(info: AnnotationInfo): Unit =

src/library/scala/annotation/nowarn.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@ package scala.annotation
1414

1515
/** An annotation for local warning suppression.
1616
*
17-
* The optional `value` parameter allows selectively silencing messages, see `scalac -Wconf:help`
18-
* for help. Examples:
17+
* The optional `value` parameter allows selectively silencing messages. See `-Wconf:help` for help
18+
* on message filter expression, or use `@nowarn("verbose")` / `@nowarn("v")` to display message
19+
* filters applicable to a specific warning.
20+
*
21+
* Examples:
1922
*
2023
* {{{
2124
* def f = {
2225
* 1: @nowarn // don't warn "a pure expression does nothing in statement position"
2326
* 2
2427
* }
2528
*
29+
* // show the warning, plus the applicable @nowarn / Wconf filters ("cat=other-pure-statement", ...)
30+
* @nowarn("v") def f = { 1; 2 }
31+
*
2632
* @nowarn def f = { 1; deprecated() } // don't warn
2733
*
2834
* @nowarn("msg=pure expression does nothing")

test/files/neg/nowarnRangePos.check

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
nowarnRangePos.scala:84: warning: A try without a catch or finally is equivalent to putting its body in a block; no exceptions are handled.
2+
Applicable -Wconf / @nowarn filters for this warning: msg=<part of the message>, cat=other, site=C.T12.f
3+
@nowarn("v") def f = try 1
4+
^
15
nowarnRangePos.scala:11: warning: method dep in class C is deprecated (since 1.2.3): message
26
@nowarn @ann(dep) def t2 = 0 // deprecation warning, @nowarn unused
37
^
@@ -19,6 +23,10 @@ nowarnRangePos.scala:75: warning: a pure expression does nothing in statement po
1923
nowarnRangePos.scala:80: warning: method dep in class C is deprecated (since 1.2.3): message
2024
a + dep
2125
^
26+
nowarnRangePos.scala:90: warning: a pure expression does nothing in statement position; multiline expressions might require enclosing parentheses
27+
Applicable -Wconf / @nowarn filters for this warning: msg=<part of the message>, cat=other-pure-statement, site=C.T13.g
28+
def g = { 1; 2 }
29+
^
2230
nowarnRangePos.scala:45: warning: I3b has a valid main method (args: Array[String]): Unit,
2331
but C.I3b will not have an entry point on the JVM.
2432
Reason: companion is a trait, which means no static forwarder can be generated.
@@ -43,6 +51,9 @@ nowarnRangePos.scala:24: warning: @nowarn annotation does not suppress any warni
4351
nowarnRangePos.scala:65: warning: @nowarn annotation does not suppress any warnings
4452
@nowarn("msg=something else") // unused
4553
^
54+
nowarnRangePos.scala:91: warning: @nowarn annotation does not suppress any warnings
55+
@nowarn("v") def unused = 0
56+
^
4657
error: No warnings can be incurred under -Werror.
47-
14 warnings
58+
17 warnings
4859
1 error

test/files/neg/nowarnRangePos.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ class C {
7979
val a = dep: @nowarn
8080
a + dep
8181
}
82+
83+
@nowarn object T12 {
84+
@nowarn("v") def f = try 1
85+
def g = { 1; 2 }
86+
}
87+
88+
@nowarn("verbose") object T13 {
89+
@nowarn def f = try 1
90+
def g = { 1; 2 }
91+
@nowarn("v") def unused = 0
92+
}
8293
}
8394

8495
trait T {

0 commit comments

Comments
 (0)
0