Commit 37d9640
authored
[Semester Project] Add new front-end phase for unused entities and add 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
@anatoliykmetyukFile tree
17 files changed
+1416
-3
lines changed- compiler/src/dotty/tools/dotc
- config
- core
- transform
- tests/neg-custom-args/fatal-warnings
- i15503-scala2
17 files changed
+1416
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
| 158 | + | |
| 159 | + | |
158 | 160 | | |
159 | 161 | | |
160 | 162 | | |
161 | | - | |
| 163 | + | |
162 | 164 | | |
163 | 165 | | |
164 | 166 | | |
165 | | - | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
166 | 193 | | |
167 | 194 | | |
168 | 195 | | |
| 196 | + | |
169 | 197 | | |
170 | 198 | | |
171 | 199 | | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
172 | 222 | | |
173 | 223 | | |
174 | 224 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
189 | 190 | | |
190 | 191 | | |
191 | 192 | | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
192 | 206 | | |
193 | 207 | | |
194 | 208 | | |
| |||
270 | 284 | | |
271 | 285 | | |
272 | 286 | | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
273 | 290 | | |
274 | 291 | | |
275 | 292 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1001 | 1001 | | |
1002 | 1002 | | |
1003 | 1003 | | |
| 1004 | + | |
1004 | 1005 | | |
1005 | 1006 | | |
1006 | 1007 | | |
| |||
0 commit comments