Broken Windows Theory: clean codebases that keep warnings under control encourage to keep the codebase clean.
-Werror can often not be enabled, for example due to non-avoidable deprecation warnings when cross-building.
This proposal has two goals: make warnings globally configurable using compiler flags, and allow suppressing warnings locally.
@unchecked in pattern matching@unused: omit -Ywarn-unused / -Xlint:unused warnings for annotated declarations (added in https://github.com/scala/scala/pull/7623, discussion at https://github.com/scala/bug/issues/10790)@migration-Xmigration:<fromScalaVersion>, show warnings that have a version bigger than fromScalaVersionprivate[scala]Global Configuration: allow users to configure what is reported as error, warning, info, or not at all.
-Werror, which escalates all warnings-Wconfig was proposed in https://github.com/scala/scala/pull/7790#issuecomment-474102186@restricted warningssince version -- for cross-building, upgrading (adriaan has a WIP, see below)since is not only used in standard library, each library has its own version numbersdeprecated says "Library authors should prepend the name of their library to the version number"-WManaging many errors/warnings
-Xmaxerrs, -Xmaxwarns-deprecation and -feature to show all warnings, one-line-summary otherwise-Werror still breaks https://github.com/scala/bug/issues/8829Local suppression
@SuppressWarnings, @silent (see below)@unchecked annotation to work for an annotated scope, e.g. @unchecked class C { ... } for all code in a class-Xreporter command line-Xsuppress-message-ids, -Xsuppress-warning-kinds global flags. dotty has error/warning ids and kinds (not sure what kinds are).-deprecation-suppress flag. conditions on: since, definition package, use site packageforRemoval, sinceXlint check to ensure that @deprecated has explicit arguments@restricted / @compileTimeOnly as generalization of @deprecated to mark APIsseverity parameter. (should we do that? or make them warnings, allow users to change severity?)@deprecated with configurable reporting is enough / better than a new annotation (https://github.com/scala/scala/pull/7790#issuecomment-472843535).Java: @java.lang.SuppressWarnings
unchecked, deprecated, lint warnings (see javac -X to get the list). Eclipse supports may more (https://stackoverflow.com/questions/1205995/what-is-the-list-of-valid-suppresswarnings-warning-names-in-java)Silencer https://github.com/ghik/silencer
@silent for scoped suppression@silent("deprecated") for message pattern, regular expresisonWartremover http://www.wartremover.org/
wartremoverErrors ++= Warts.unsafe, wartremoverWarnings ++= ...@SuppressWarnings(Array("org.wartremover.warts.Var", "org.wartremover.warts.Null")) on declarationwartremoverExcluded += baseDirectory.value / "src" / "main" / "scala" / "SomeFile.scala" in sbtDownstream impact
Better warning & error messages (considered out of scope / separate proposal)
-explain, https://github.com/softwaremill/scala-clippy)Syntax needs to be clarified and probably made less ambiguous, so far i only thought about categories, filters and severities.
-Wconf <config>
lint-x for every -Xlint:<x>, lint for all lintsx for every -W<x> (dead-code, value-discard, unused)deprecationlanguage-x for every -language:<x>, language for all feature flagsany: matches any warning, whether or not it's in one of the categories above - filterspos:com.package.Class where the warning is triggered, displayedmsg:expr -- need to decide details (full regex? only simple wildcard? substring match?)from:com.package.Class where the deprecated entity is declaredsince<2.12. see https://github.com/scala/scala/pull/7728/files#diff-3da64c3c223da7bbf9970ac26f9fb9b8R108. examples: since<Library 2.3, since<*2.3 to match any character prefix ("foo bar 2.3"), seems useful in combination with from:pkge(rror)w(arning) / w-summary / w-groupi(nfo) / i-summary / i-group: show, but don't fail on -Werror (useful?)s(ilent)warning and info:summary: there were n warnings, per category.group: show every warning with the same mesage once, mention how many instances.Note: this only configures how warnings are emitted. -Xlint, -Wunused, Wdead-code etc are still required to make the compiler do the additional checks and emit warnings.
First matching rule wins
Relation to existing functionality
-Wconf deprecation:language:warning-summary,any:warning-Wconf adds rules on the left, i.e., the default comes last-deprecation is the same as -Wconf deprecation:warning-feature is -Wconf language:warning-language:x is -Wconf language-x:silentimport scala.language.x is @silent("language-x") (see below)Examples
-Wconf any:pos:com.corp.yolo:silent-Wconf deprecation:from:org.fancy.library:since<*2.2:e,deprecation:i-group error on some deprecations, info-group others-Wconf 'any:msg:pure expression does nothing in statement position:s'@silent: basically the same as https://github.com/ghik/silencer#annotation-based-suppression
@silent class C, @silent def f{foo; bar}: @silent@silent("deprecated"), @silent("deprecated,language-higherKinds")@silent(msg="comparing true and false will always")@silent doesn't silence anythingperhaps the right context to tackle this would be in association with backporting the structured-errors stuff form Dotty
This would be useful for tools like scalafix. In particular, it would be useful to have the ability override the severity level per message kind, despite presence of -Xfatal-warnings. It would also be useful to have unique identifiers attached to each reported message (cc/ https://github.com/scalameta/scalameta/issues/924)
Unique identifiers would be appreciated for scala-clippy as well. Currently we are using regexes to try to determine the message type.
The other thing that is sorely lacking currently is a general escape hatch mechanism usable per-use-site, e.g. @SuppressWarnings(Seq(CompilerWarning.MissingInterpolator)). Currently people often have to globally disable useful warning because of a tiny number of false positives.
I think having @warnoff or @suppressWarnings in the standard library would be very useful. Currently, linters are forced to use comments because if they used their own annotation that would add a runtime dependency on downstream users. This approach would be much extensible and elegant and could enable other tools to reuse them.
Agree. Wartremover uses @SuppressWarnings rather than comments, but only because it has to implement that itself. scalastyle uses comments, as you say, and it's very fragile/dangerous. It would be amazing if there was a single annotation where you could specify the particular warnings to silence, and it was respected by scalac, scalafix linters, wartremover, ...
Get excited? https://github.com/lampepfl/dotty/pull/5337
Maybe bundled with this we can do https://github.com/scala/bug/issues/8829.
Most helpful comment
The other thing that is sorely lacking currently is a general escape hatch mechanism usable per-use-site, e.g.
@SuppressWarnings(Seq(CompilerWarning.MissingInterpolator)). Currently people often have to globally disable useful warning because of a tiny number of false positives.