Giter Club home page Giter Club logo

scalafix-unused's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

scala-steward

scalafix-unused's Issues

Better lint message

For reference, in scala2

Imports

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 {
       ^

Locals

❯ 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
                   ^

Params

❯ 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
           ^

PatVars

❯ 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
            ^

Privates

❯ 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
                 ^

Shouldn't warn unused param for proof

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 =:=

False positive warning on Enums

Describe the bug

Enum shouldn't be asserted if the children are referred.

Installation:

  • Scala version: v3.0.1

Minimal Reproduciable code

  def localEnum =
    enum Foo: // shouldn't be asserted
      case A, B
    println(Foo.A)

Support local import

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)
  }
}

Support given imports

Describe the bug

Installation:

  • Scala version: v3.0.1

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)

Warn if var never get (if there's only set operation)?

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

Don't warn unused params in override method

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.

Better Anonymous given warning message

Describe the bug

Installation:

  • Scala version: v3.0.1

Minimal Reproduciable code

package fix.example
package angiven

trait Foo

def bar(using Foo) = 42 /* assert: Unused
              ^^^
Unused parameter ''
*/
// what name should be here?

False positive unused warning for implicit/context params and implicit conversion

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:

  • Scala version: v3.0.1

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"
}

Add test for macro expansion (in Scala2)

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
}

Type definition / import doesn't marked as used because of lack of symbol occurrence

Describe the bug

Installation:

  • Scala version: v3.0.1

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 = ???
}

Don't warn if private fields are accessed from companion object/class

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

False negative if unused parameter is referred by by name arguments

(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
  )

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.