-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement linter options #15503
Comments
If this project is taken as a semester project, we would have to synchronize with the state when the project starts. reserving the project as a semester project should not hold up progress that could be made before. |
Note that there is already a PR for unused imports: #14524 |
Yes, @som-snytt what's the status on that? I notice it is still in draft state? |
I can probably make some time to help with that one if needed. |
I'll rebase |
Project reserved for Paul Cole. |
See #15885 for another possible linter task. |
…d support for unused imports (#16157) This PR, related to my **semester project** #15503 on adding **dotty's linter features**, adds the following: - [x] Add the `CheckUnused` front-end phase, which will check the the tree produced by the typer, for unused entites (imports, local defs, ...) - [x] Emit warning for `-Wunused:imports` including **given imports** and **wildcard imports** - [x] Emit warning for `-Wunused:locals` - [x] Emit warning for `-Wunused:privates` - [x] Emit warning for `-Wunused:params` - ~~Emit warning for `-Wunused:patvars`~~ - [x] Emit warning for `-Wunused:unsafe-warn-patvars` - [x] Emit warning for `-Wunused:linted` - [x] Add a simple _fatal-warning_ compilation-test suit - [x] _Fixes for the warning format_ - [x] Better help in CLI for `-Wunused` - [x] Add `-Wunused:givens` alias to `-Wunused:implicits` Here are a few examples: #### Unused Imports ```scala object Foo { import collection.mutable.{Set, Map} def main(args: Array[String]) = val bar = Set("this","set","is","used") println(s"Hello World: $bar") } ``` ``` sbt:scala3> scalac -Wunused:imports ../Foo.scala [...] -- Warning: ../scratch_files/Hello.scala:2:34 ---------------------------------- 2 | import collection.mutable.{Set, Map} | ^^^ | unused import 1 warning found ``` #### Unused local definitions ```scala class Foo { def bar = val a = 1 2 + 2 } ``` ``` sbt:scala3> scalac -Wunused:locals ../Foo.scala [...] -- Warning: ../scratch_files/MyHello.scala:3:8 --------------------------------- 3 | val a = 1 | ^^^^^^^^^ | unused local definition 1 warning found ``` #### Unused private members ```scala class Foo { private def a = 1 private def b = 2 def doSomething = b } ``` ``` sbt:scala3> scalac -Wunused:privates ../Foo.scala [...] -- Warning: ../scratch_files/MyHello.scala:2:14 -------------------------------- 2 | private def a = 1 | ^^^^^^^^^^^^^^^^^ | unused private member 1 warning found ``` #### Unused parameters ```scala def foo(a: String)(using Int) = bar ``` ``` sbt:scala3> scalac -Wunused:params ../scratch_files/Foo.scala [...] -- Warning: ../scratch_files/MyHello.scala:1:8 --------------------------------- 1 |def foo(a: String)(using Int) = bar | ^ | unused explicit parameter -- Warning: ../scratch_files/MyHello.scala:1:25 -------------------------------- 1 |def foo(a: String)(using Int) = bar | ^ | unused implicit parameter 2 warnings found ``` #### Unused pattern variables ```scala def foo(a: List[Int]) = a match case head :: tail => ??? case Nil => ??? ``` ``` sbt:scala3> scalac -Wunused:unsafe-warn-patvars ../scratch_files/Foo.scala [...] -- Warning: ../scratch_files/MyHello.scala:2:9 --------------------------------- 2 | case head :: tail => ??? | ^^^^ | unused pattern variable -- Warning: ../scratch_files/MyHello.scala:2:17 -------------------------------- 2 | case head :: tail => ??? | ^^^^ | unused pattern variable 2 warnings found ``` Please check the [test file](tests/neg-custom-args/fatal-warnings/i15503/i15503a.scala) for the handled cases. @odersky @anatoliykmetyuk
…7160) This PR is related to my Bachelor Semester Project, supervised by @anatoliykmetyuk. The latter consist in improving and implementing more Scala 3 linter options (see #15503), with #16639 as a starting issue fixed in this PR. - During the traversal in CheckUnused.scala (Miniphase & local TreeTraverser), when reaching an `Assign` case, symbols are collected as set, and then used to filter used locals and privates variable at reporting time. - Adapt test suit, and Add more test accordingly. - Note that for a same variable the unused warning always has priority and shadows the unset warning. That feature follows the Scala 2 `-Ywarn-unused:<args>` behavior.
As of #16157 this is now implemented, so I'll go ahead and close! if there are any issues or specific follow-ups to this needed, please do create issues for them. |
Scala 3 is currently missing several of Scala 2's linter options, including:
It would be great to add these.
We could also look out for other valuable options to add.
The text was updated successfully, but these errors were encountered: