tanishiking / scalafix-unused Goto Github PK
View Code? Open in Web Editor NEW[WIP] Scalafix rule for warning unused values and imports that works both in Scala2 and Scala3.
License: Apache License 2.0
[WIP] Scalafix rule for warning unused values and imports that works both in Scala2 and Scala3.
License: Apache License 2.0
Minimal Reproducible code
class Main {
def main(args: Array[String]): Unit = println("hello, args") // no warn
}
Minimal Reproducible code
def f = (i: Int) => 42 // should warn
For reference, in scala2
main/scala/fix on main [!] via 🆂 v2.13.6 took 13s
❯ scala -Xlint:unused Imports.scala
Imports.scala:11: warning: Unused import
import scala.collection.immutable.HashMap /* assert: Unused
^
Imports.scala:16: warning: Unused import
import scala.collection.mutable /* assert: Unused
^
Imports.scala:22: warning: Unused import
import util.Try /* assert: Unused
^
Imports.scala:26: warning: Unused import
import util.{Success => uSuccess} /* assert: Unused
^
Imports.scala:31: warning: Unused import
import scala.math.{max, min} /* assert: Unused
^
Imports.scala:37: warning: Unused import
import scala.concurrent._ /* assert: Unused
^
Imports.scala:42: warning: private object Imports in <$anon: AnyRef> is never used
object Imports {
^
❯ scala -Xlint:unused Locals.scala
Locals.scala:11: warning: private object Locals in <$anon: AnyRef> is never used
object Locals {
^
Locals.scala:14: warning: local val x in method complete is never used
val x = 1 /* assert: Unused
^
Locals.scala:21: warning: local var x in method variable is never used
var x = 1 /* assert: Unused
^
Locals.scala:28: warning: local val x in method implicit1 is never used
implicit val x = 1 /* assert: Unused
^
❯ scala -Wunused:params Params.scala
Params.scala:13: warning: parameter value isSuccess in method complete is never used
def complete(isSuccess: Boolean): Unit = () /* assert: Unused
^
Params.scala:17: warning: parameter value param in method f2 is never used
def f2(param: Boolean): Boolean = { /* assert: Unused
^
❯ scala -Wunused:patvars PatVars.scala
PatVars.scala:22: warning: pattern var x in method pattern1 is never used: use a wildcard `_` or suppress this warning with `x@_`
case A(x) => () /* assert: Unused
^
PatVars.scala:26: warning: pattern var bbb in method pattern1 is never used: use a wildcard `_` or suppress this warning with `bbb@_`
case B(aaa, bbb) => println(aaa) /* assert: Unused
^
PatVars.scala:30: warning: pattern var c in method pattern1 is never used: use a wildcard `_` or suppress this warning with `c@_`
case c @ C(x, y) => println(x + y) /* assert: Unused
^
PatVars.scala:37: warning: pattern var b in method patterns2 is never used: use a wildcard `_` or suppress this warning with `b@_`
val (a, b, _) = (1, 2, 3) /* assert: Unused
^
❯ scala -Wunused:privates Privates.scala
Privates.scala:13: warning: private object PrivatesPositive in <$anon: AnyRef> is never used
object PrivatesPositive {
^
Privates.scala:15: warning: private method foo in object PrivatesObj is never used
private def foo(x: Int) = ??? /* assert: Unused
^
Privates.scala:20: warning: private object Foo in object PrivatesObj is never used
private object Foo { /* assert: Unused
^
Privates.scala:32: warning: private val field in object PrivatesObj is never used
private val field = 1 /* assert: Unused
^
Privates.scala:39: warning: private method foo in trait PrivatesTrait is never used
private def foo(x: Int) = ??? // assert: Unused
^
Privates.scala:40: warning: private object Foo in trait PrivatesTrait is never used
private object Foo { // assert: Unused
^
Privates.scala:44: warning: private val field in trait PrivatesTrait is never used
private val field = 1 // assert: Unused
^
Privates.scala:48: warning: private method foo in class PrivateClass is never used
private def foo(x: Int) = ??? // assert: Unused
^
Privates.scala:49: warning: private object Foo in class PrivateClass is never used
private object Foo { // assert: Unused
^
Privates.scala:53: warning: private val field in class PrivateClass is never used
private val field = 1 // assert: Unused
^
Privates.scala:58: warning: private object PrivateNegative in <$anon: AnyRef> is never used
object PrivateNegative {
^
Privates.scala:79: warning: private var field in object PrivatesObj is never updated: consider using immutable val
private var field = 1
^
Privates.scala:27: warning: private type A in object PrivatesObj is never used
private type A = Int /* assert: Unused
^
Privates.scala:43: warning: private type A in trait PrivatesTrait is never used
private type A = Int // assert: Unused
^
Privates.scala:52: warning: private type A in class PrivateClass is never used
private type A = Int // assert: Unused
^
Minimal Reproducible code
trait Proofs {
def f[A, B](implicit ev: A =:= B) = 42
def g[A, B](implicit ev: A <:< B) = 42
def f2[A, B](ev: A =:= B) = 42
def g2[A, B](ev: A <:< B) = 42
}
Expected behavior
Maybe we should suppress error if the type contains something like =:=
Describe the bug
Enum shouldn't be asserted if the children are referred.
Installation:
Minimal Reproduciable code
def localEnum =
enum Foo: // shouldn't be asserted
case A, B
println(Foo.A)
Describe the bug
It doesn't warn local imports if there's a reference from other scopes. Should warn if local imports are unused in the scope.
Minimal Reproducible code
trait Warn {
{
import p1.A // should warn, but it doesn't because p1.A is accessed from the following scope
println(123)
}
{
import p1.{ A, B } // assert: Unused
println("abc".bippy)
}
}
Minimal Reproduciable code
object `classof something` {
private class intrinsically // shouldn't be asserted here
def f = classOf[intrinsically].toString()
}
Expected behavior
\
scalafix-unused/rules/src/main/scala/fix/UnusedSymbol.scala
Lines 24 to 33 in b29ec64
Idea comes from: scala/bug#11856
Describe the bug
Installation:
Minimal Reproduciable code
object A:
class TC
given tc: TC = ???
given ti: Int = 1
given ts: String = ""
def f(using TC) = ???
val v = 1
object B:
import A.*
import A.given
def print =
f(using tc)
println(v)
object C:
import A.*
import A.given // TODO: should be asserted
def print =
println(v)
object D:
// TODO: given Int should be asserted
// TODO given String shouldn't be asserted
import A.given Int
import A.given String // assert: Unused
println(ts)
Minimal Reproducible code
class StableAccessors {
private var s1: Int = 0 // assert: Unused
// TODO?
private var s2: Int = 0 // warn, never set
// TODO: warn because s3 never get
private var s3: Int = 0
private var s4: Int = 0 // no warn
def bippy(): Int = {
s3 = 5
s4 = 6
s2 + s4
}
}
The behavior should be consistent with Scala2
Minimal Reproduciable code
trait InterFace {
/** Call something. */
def call(a: Int, b: String, c: Double): Int
}
trait BadAPI extends InterFace {
def f(a: Int,
b: String, // assert: Unused
c: Double): Int = {
println(c)
a
}
override def call(a: Int,
b: String, // no warn, required by superclass
c: Double): Int = {
println(c)
a
}
}
Expected behavior
override method is sometimes necessary to have unused parameters which are required in a superclass. Therefore, we should skip the check for override methods.
Describe the bug
Installation:
Minimal Reproduciable code
package fix.example
package angiven
trait Foo
def bar(using Foo) = 42 /* assert: Unused
^^^
Unused parameter ''
*/
// what name should be here?
Describe the bug
This is because of the lack of synthetics
section in SemanticDB generated from Scala3, the first step to support this feature is merging tanishiking/dotty#2
Installation:
Minimal Reproduciable code
package fix.example
package example
import scala.math.Ordered.orderingToOrdered // this shouln't be asserted
class Issue1749 {
val x1 = 42
val x2 = 42
(x1, x1)
.compare((x2, x2))
}
class Issue1854 {
val map = collection.mutable.Map.empty[String, String]
map("a") = "b"
}
See: https://github.com/scala/scala/tree/2.13.x/test/files/neg/warn-unused-imports
Check if there's no warn for unused import in macro expansion tree
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
object Macro {
def f: Int = macro fImpl
def fImpl(c: Context): c.Tree = {
import c.universe._
q"""
import scala.util.Random
42 // TODO randomize
"""
}
}
class MacroClient {
def x = Macro.f // don't crash; but also don't warn on expansion, see scala/bug#10270 and [pos|neg]/t10270
}
Describe the bug
Installation:
Minimal Reproduciable code
package fix.example
package prefixes3
class C {
type T
def m1: T = ???
object N {
type U
}
def k1: N.U = ???
}
object Test {
val c: C = ???
def m2: c.T = ???
def k2: c.N.U = ???
// TODO: fix c.N._ shouldn't be reported
// this is because `U` doesn't have symbol
// see: https://github.com/lampepfl/dotty/blob/e7a641c5ad61fa683423954fa9263079a890c809/tests/semanticdb/expect/Prefixes.expect.scala#L27
// after fix the issue, merge Prefixes.scala and this file
//
import c.N._
def k3: U = ???
def n2: M.T = ???
import M._
def n3: T = ???
}
Minimal Reproducible code
class Bippy(a: Int, b: Int) {
private def this(c: Int) = this(c, c)
final private val HI_COMPANION: Int = 500 // no warn, accessed from companion
def hi() = Bippy.HI_INSTANCE
}
object Bippy {
def hi(x: Bippy) = x.HI_COMPANION
// TODO
// no warn, accessed from instance
private val HI_INSTANCE: Int = 500 // assert: Unused
private val HEY_INSTANCE: Int = 1000 // assert: Unused
private lazy val BOOL: Boolean = true // assert: Unused
}
HI_INSTNACE
shouldn't be asserted because it's accessed from from def hi
(low priority)
Minimal Reproduciable code
abstract class Issue2116 {
def check(
includeDocs: Boolean = false, // assert: Unused
includeCommitCharacter: Boolean = false // unreported because there's named argument
)(implicit loc: ExecutionContext): Unit = {} // assert: Unused
}
class Issue2116_2 extends Issue2116 {
implicit val ec = scala.concurrent.ExecutionContext.global
check(
includeCommitCharacter = true
)
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.