Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Scala compiler plugin for warning suppression

License

NotificationsYou must be signed in to change notification settings

ghik/silencer

Repository files navigation

Build StatusMaven Central

Compatibility

Silencer is available for Scala 2.11, 2.12, and 2.13.

NOTE: Scala 2.13.2 and 2.12.13 introducedconfigurable warnings.This means that unless you're still cross compiling for Scala 2.11, this plugin is obsolete, and you should use@nowarn.

If you're still cross compiling for 2.11 then this plugin can be used in conjunction withscala-collection-compat in order to suppress warnings in allScala versions using@nowarn.

As a compiler plugin, Silencer must be separately built for every minor version of Scala. If you find that Silencer is notavailable for your version of Scala (most likely some newly released one), please contribute - the instructions on how to do it are below :)

How to add support for a new version of Scala

  1. Fork and clone the repository.
  2. Editbuild.sbt and add the new Scala version tocrossScalaVersions. Make sure to keep the order of the list,which should start with the newest version.
  3. Invokesbt githubWorkflowGenerate
  4. Commit changes inbuild.sbt and Github workflow definitions
  5. Make a PR :)

Setup

If you're using SBT, add this to your project definition:

ThisBuild/ libraryDependencies++=Seq(  compilerPlugin("com.github.ghik"%"silencer-plugin"% silencerVersion crossCrossVersion.full),"com.github.ghik"%"silencer-lib"% silencerVersion%Provided crossCrossVersion.full)

If you're using Gradle:

ext {    scalaVersion="..."// e.g. "2.13.0"    silencerVersion="..."// appropriate silencer version}configurations {    scalacPlugin {        transitive=false    }}dependencies {    compile"com.github.ghik:silencer-lib_$scalaVersion:$silencerVersion"    scalacPlugin"com.github.ghik:silencer-plugin_$scalaVersion:$silencerVersion"}tasks.withType(ScalaCompile) {    scalaCompileOptions.additionalParameters=            configurations.scalacPlugin.collect {"-Xplugin:"+ it.absolutePath }}

Note that since bothsilencer-plugin andsilencer-lib are compile time only dependencies, Silencer can be usedin ScalaJS and Scala Native without having to be cross compiled for them.

Annotation-based suppression

With the plugin enabled, warnings can be suppressed using the@com.github.ghik.silencer.silentor@scala.annotation.nowarn annotation.It can be applied on a single statement or expression, entiredef/val/var definition or entireclass/object/trait definition.

importcom.github.ghik.silencer.silent@silentclasssomeClass { ... }@silentdefsomeMethod()= { ... }someDeprecatedApi("something"):@silent

Message pattern

By default the@silent annotation suppressesall warnings in some code fragment. You can limit the suppression tosome specific classes of warnings by passing a message pattern (regular expression) to the annotation, e.g.

@silent("deprecated")defusesDeprecatedApi():Unit= {  someDeprecatedApi("something")}

Using@nowarn

Scala 2.13.2 and 2.12.13 introducedconfigurable warnings using-Wconfcompiler option and@scala.annotation.nowarn. annotation. For Scala 2.11, this annotation is provided by thescala-collection-compat library and interpreted by thesilencerplugin.

NOTE:@nowarn in Scala 2.13.2 supports various fine-grained filters (e.g. warning category, message pattern, etc.).Silencer only supports themsg=<pattern> filter - all other filters simply suppress everything, as if there wereno filters specified.

Detecting unused annotations

If a@silent annotation does not actually suppress any warnings, you can makesilencer report an error in suchsituation. This can be enabled by passing thecheckUnused option to the plugin:

scalacOptions+="-P:silencer:checkUnused"

Global regex-based suppression

You can also suppress warnings globally based on a warning message regex. In order to do that, pass this option toscalac:

scalacOptions+="-P:silencer:globalFilters=<semicolon separated message regexes>"

Line content based suppression

Filtering may also be based on the content of source line that generated the warning.This is particularly useful for suppressing 'unused import' warnings based on what's being imported.

scalacOptions+="-P:silencer:lineContentFilters=<semicolon separated line content regexes>"

Filename based suppression

Another option is to suppress all warnings in selected source files. This can be done by specifying a list of file path regexes:

scalacOptions+="-P:silencer:pathFilters=<semicolon separated file path regexes>"

NOTE: In order to make builds independent of environment, filename separators are normalized to UNIX style (/)before the path is matched against path patterns.

By default, absolute file path is matched against path patterns. In order to make your build independent of where yourproject is checked out, you can specify a list of source root directories. Source file paths will be relativized withrespect to them before being matched against path patterns. Usually it should be enough to pass project base directoryas source root (i.e.baseDirectory.value in SBT):

scalacOptions+=s"-P:silencer:sourceRoots=${baseDirectory.value.getCanonicalPath}"

Another good choice for source roots may be actual SBT source directories:

scalacOptions+=s"-P:silencer:sourceRoots=${sourceDirectories.value.map(_.getCanonicalPath).mkString(";")}"

Searching macro expansions

By default (starting from version 1.6.0) silencer does not look for@silent annotations in macro expansions.If you want to bring back the old behaviour where both macro expansions and expandees are searched, use the-P:silencer:searchMacroExpansions option.


[8]ページ先頭

©2009-2025 Movatter.jp