typelevel-scalafix icon indicating copy to clipboard operation
typelevel-scalafix copied to clipboard

TypelevelAs should add `cats.syntax` import

Open NeQuissimus opened this issue 3 years ago • 9 comments

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.

NeQuissimus avatar Jul 26 '22 18:07 NeQuissimus

It does, if you do import cats.syntax.all._ :) I think there's a way for scalafix to add imports if necessary.

armanbilge avatar Jul 26 '22 18:07 armanbilge

Oh, that is possible. I wonder if there is a good way to make that part of the message in such a situation maybe?

My actual issue turned out to be that I had multiple Functor instances (from Temporal and Sync) and the void syntax could not decide which one to pick :)

NeQuissimus avatar Jul 26 '22 20:07 NeQuissimus

Here's how an http4s scalafix adds an import if needed. https://github.com/typelevel/typelevel-scalafix/blob/8e246387dbed1e772933d2f757bc7609bc909861/modules/http4s/rules/src/main/scala/org/typelevel/fix/Http4sLiteralsSyntax.scala#L41-L43

armanbilge avatar Jul 26 '22 20:07 armanbilge

Let's close this issue because there is no issue :)

NeQuissimus avatar Jul 28 '22 15:07 NeQuissimus

Actually there is an issue, the import is not being added :)

armanbilge avatar Jul 28 '22 15:07 armanbilge

Right but you'd have to make the rule a lot more specific then, would you not? What would happen to a foo.map(_ => ()) where foo does not have a void in cats.syntax?

NeQuissimus avatar Jul 28 '22 15:07 NeQuissimus

Good point. So even after adding the import, this Scalafix could still be suggesting broken code. Seems to me like another reason to keep it open.

armanbilge avatar Jul 28 '22 15:07 armanbilge

👍 I agree with that. This almost needs something like "do we have an implicit Functor? OK, just add the syntax" and otherwise bail out.

NeQuissimus avatar Jul 28 '22 15:07 NeQuissimus

The (WIP) port of the RemoveInstancesImport rule does something similar to that actually: https://github.com/typelevel/typelevel-scalafix/pull/31/files#diff-b2f050feb2124095e5835138e89f5f116e4781fc5aeb560ea4415dcf7f17f926R46

DavidGregory084 avatar Jul 28 '22 15:07 DavidGregory084