Skip to content

Commit

Permalink
Merge pull request #1992 from bjaglin/doc-removeunused
Browse files Browse the repository at this point in the history
improve docs about RemoveUnused
  • Loading branch information
bjaglin committed May 1, 2024
2 parents b5230fd + b6f35b3 commit 2ca0d85
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
17 changes: 16 additions & 1 deletion docs/rules/RemoveUnused.md
Expand Up @@ -132,7 +132,22 @@ You may request more granular warnings to the compiler if you opt-out
from some rewrites in the rule configuration.

```
$ scala212 -Wunused:help
$ scala212 -Ywarn-unused:help
Enable or disable specific `unused' warnings
imports Warn if an import selector is not referenced.
patvars Warn if a variable bound in a pattern is unused.
privates Warn if a private member is unused.
locals Warn if a local definition is unused.
explicits Warn if an explicit parameter is unused.
implicits Warn if an implicit parameter is unused.
nowarn Warn if a @nowarn annotation does not suppress any warnings.
params Enable -Ywarn-unused:explicits,implicits.
linted -Xlint:unused.
Default: All choices are enabled by default.
```

```
$ scala213 -Wunused:help
Enable or disable specific `unused` warnings
imports Warn if an import selector is not referenced.
patvars Warn if a variable bound in a pattern is unused.
Expand Down
22 changes: 16 additions & 6 deletions docs/users/installation.md
Expand Up @@ -62,18 +62,18 @@ Next, if we run another rule like `RemoveUnused` then we get an error
```
> myproject/scalafix RemoveUnused
[error] (Compile / scalafix) scalafix.sbt.InvalidArgument: 2 errors
[E1] The semanticdb-scalac compiler plugin is required to run semantic
[E1] The scalac compiler should produce semanticdb files to run semantic
rules like RemoveUnused ...
[E2] The Scala compiler option "-Ywarn-unused" is required to use
RemoveUnused ...
[E2] A Scala compiler option is required to use RemoveUnused. To fix this
problem, update your build to add -Ywarn-unused-import (with 2.12) ...
```

The first error message means the
[SemanticDB](https://scalameta.org/docs/semanticdb/guide.html) compiler plugin
is not enabled for this project. The second error says `RemoveUnused` requires

the Scala compiler option `-Ywarn-unused-import` (or `-Wunused:imports` in
2.13.x). To fix both problems, add the following settings to `build.sbt`
2.13.x or 3.x). To fix both problems, add the following settings to `build.sbt`

```diff
/*
Expand All @@ -90,7 +90,12 @@ the Scala compiler option `-Ywarn-unused-import` (or `-Wunused:imports` in
)

lazy val myproject = project.settings(
scalacOptions += "-Ywarn-unused-import" // required by `RemoveUnused` rule
+ scalacOptions += {
+ if (scalaVersion.value.startsWith("2.12"))
+ "-Ywarn-unused-import"
+ else
+ "-Wunused:imports"
+ }
)
```

Expand All @@ -104,7 +109,12 @@ the Scala compiler option `-Ywarn-unused-import` (or `-Wunused:imports` in
scalaVersion := "@SCALA212@", // @SCALA213@, or 3.x
+ semanticdbEnabled := true, // enable SemanticDB
+ semanticdbVersion := scalafixSemanticdb.revision, // only required for Scala 2.x
+ scalacOptions += "-Ywarn-unused-import" // Scala 2.x only, required by `RemoveUnused`
+ scalacOptions += {
+ if (scalaVersion.value.startsWith("2.12"))
+ "-Ywarn-unused-import"
+ else
+ "-Wunused:imports"
+ }
)
```

Expand Down
Expand Up @@ -916,7 +916,7 @@ object OrganizeImports {
Configured.error(
"A Scala compiler option is required to use OrganizeImports with"
+ " \"OrganizeImports.removeUnused\" set to true. To fix this problem, update your"
+ " build to add `-Ywarn-unused` (2.12) or `-Wunused:imports` (2.13 and 3.4+)."
+ " build to add `-Ywarn-unused-import` (2.12) or `-Wunused:imports` (2.13 and 3.4+)."
)
else
Configured.error(
Expand Down

0 comments on commit 2ca0d85

Please sign in to comment.