Git Product home page Git Product logo

typelevel-scalafix's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

typelevel-scalafix's Issues

Add linter for any discarded value

I suggest adding a linter that will warn for any discarded value, regardless of its type. This would both catch unused effectful code that's not concrete IO - it would need to be disabled for blocks of side-effecting code, but I see this as an extra advantage since side-effecting code would have to be explicitly marked as such.

Demand for implicit Spawn[F] and Parallel[F]

Code that demands an implicit cats.effect.Spawn[F] (or a descendant) and also cats.Parallel[F] could be rewritten to demand only the Spawn[F] with an import cats.effect.instances.spawn._.

TypelevelAs should add `cats.syntax` import

Version 0.1.5

import cats.effect.Resource

val foo: Resource[A, B] = ???
val x = foo.map(_ => ())

The rule TypelevelAs.as will complain and suggest to rewrite the map line to .void.
Resource does not have a void method :)
The check should probably be limited to types that provide the necessary functions.

Lint for `.flatTap` with non-`Unit` result

flatTap is defined as:

def flatTap[A, B](fa: F[A])(f: A => F[B]): F[A] =
  flatMap(fa)(a => as(f(a), a))

https://github.com/typelevel/cats/blob/fbad4be326cb1a244bfae14db43e06cc486aa602/core/src/main/scala/cats/FlatMap.scala#L172-L173

While silently discarding an arbitrary B is convenient for ergonomics, it may be desirable to call attention to the fact you are discarding a result by requiring that B =:= Unit and thus forcing the user to call .void if their function does not return F[Unit].

This seems like a good candidate for a lint. Inspired by a Discord question, h/t @sbuzzard.

UnusedIO - MatchError: throw (e)

This is an interesting one :)

I (accidentally) ran UnusedIO against a code base that does not use IO at all.

I cannot share the code but it would appear that throw e can cause issues.

[error] Caused by: scala.MatchError: throw (e) (of class scala.meta.Term$Throw$TermThrowImpl)
[error] 	at org.typelevel.fix.UnusedIO$$anonfun$self$lzycompute$1$1.$anonfun$applyOrElse$2(UnusedIO.scala:68)
[error] 	at scala.collection.immutable.List.map(List.scala:250)
[error] 	at org.typelevel.fix.UnusedIO$$anonfun$self$lzycompute$1$1.applyOrElse(UnusedIO.scala:68)
[error] 	at org.typelevel.fix.UnusedIO$$anonfun$self$lzycompute$1$1.applyOrElse(UnusedIO.scala:49)
[error] 	at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
[error] 	at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
[error] 	at org.typelevel.fix.UnusedIO.org$typelevel$fix$UnusedIO$$checkInner$1(UnusedIO.scala:76)
[error] 	at org.typelevel.fix.UnusedIO$$anonfun$self$lzycompute$1$1.$anonfun$applyOrElse$1(UnusedIO.scala:65)
[error] 	at scala.collection.immutable.List.map(List.scala:250)
[error] 	at org.typelevel.fix.UnusedIO$$anonfun$self$lzycompute$1$1.applyOrElse(UnusedIO.scala:63)
[error] 	at org.typelevel.fix.UnusedIO$$anonfun$self$lzycompute$1$1.applyOrElse(UnusedIO.scala:49)
[error] 	at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
[error] 	at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
[error] 	at org.typelevel.fix.UnusedIO.org$typelevel$fix$UnusedIO$$checkInner$1(UnusedIO.scala:76)
[error] 	at org.typelevel.fix.UnusedIO.checkDiscardedStat(UnusedIO.scala:79)
[error] 	at org.typelevel.fix.UnusedIO$$anonfun$checkTree$1.$anonfun$applyOrElse$3(UnusedIO.scala:90)
[error] 	at scala.collection.immutable.List.map(List.scala:250)
[error] 	at org.typelevel.fix.UnusedIO$$anonfun$checkTree$1.applyOrElse(UnusedIO.scala:90)
[error] 	at org.typelevel.fix.UnusedIO$$anonfun$checkTree$1.applyOrElse(UnusedIO.scala:84)
[error] 	at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
[error] 	at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)
[error] 	at scala.meta.transversers.Api$XtensionCollectionLikeUI$traverser$3$.apply(Api.scala:32)

Try to warn on `Resource` leak

Since I don't know much about scalafix not sure how hard, or even impossible, this will be...
But, it would be great to warn / error if someone tries to leak a Resource using pure or delay inside use; which is a somewhat common beginner mistake.

Maybe a best effort that tries to catch common variations like ra.use(a => a.pure[F]) or ra.use(IO.apply) can be enough?
Another idea would be to warn if use returns an F[A] rather than some F[B] (meaning the same type of the Resource), since on the surface it seems that would never be a good idea; but I may be being too naive.

Setup a website

As we acquire more rules it might be better to set up a website with different pages and/or table-of-contents for the various modules to help navigate it all

UnusedIO: init of empty list

I am writing tests using munit.CatsEffectSuite and using typelevel-scalafix v0.1.3.

import munit.CatsEffectSuite

final class MySuite extends CatsEffectSuite {
  test("foo") {}
}

This will result in

[error] Caused by: java.lang.UnsupportedOperationException: init of empty list
[error] 	at scala.collection.immutable.Nil$.init(List.scala:666)
[error] 	at scala.collection.immutable.Nil$.init(List.scala:661)
[error] 	at org.typelevel.fix.UnusedIO$$anonfun$checkTree$1.applyOrElse(UnusedIO.scala:90)
[error] 	at org.typelevel.fix.UnusedIO$$anonfun$checkTree$1.applyOrElse(UnusedIO.scala:84)
[error] 	at scala.PartialFunction$Lifted.apply(PartialFunction.scala:338)
[error] 	at scala.PartialFunction$Lifted.apply(PartialFunction.scala:334)

Idea: show interpolation without variables

A simple rule that looks at show"foobar" strings and warns if there is no variable inside the interpolation.

val foo = "foo"
val a = show"$foo" // Should pass
val b = show"foo" // Should fail
val c = show"${foo}" // Should pass

Artifacts cannot be found

@armanbilge @DavidGregory084

Hey, I tried adding these Scalafix rules to a project of mine.
I added resolvers += Resolver.sonatypeRepo("snapshots") into my project/plugins.sbt but the artifacts cannot be found.
Browsing https://oss.sonatype.org/content/repositories/snapshots/org/typelevel/ does not show any typelevel-scalafix folders at all.
Have the latest snapshots been expired? :)
Could you maybe publish a 0.1 tag to push a JAR into the release channel?

Thank you :)

UnusedIO improvements

I am just running through a few test scenarios :)

Detected:

  def test(): IO[Long] = for {
    a <- IO.pure(42L)
    _ = IO.println("hello")
  } yield a

Not detected:

  def test(): IO[Long] = for {
    a <- IO.pure(42L)
    _ = IO { println("hello") }
  } yield a

Not detected:

  def test(): Long = {
    IO { println("hello") }
    42
  }

So far the rule definitely helps with common cases of "forgotten IO" :)

Just leaving these here as potential test cases for future improvements.

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.