scalafix icon indicating copy to clipboard operation
scalafix copied to clipboard

Add a linter for discarded values

Open bplommer opened this issue 2 years ago • 4 comments

I suggest adding a linter that warns about discarded values regardless of type, for use with pure functional code. This is in a sense a generalisation of scala's built-in "discarded non-unit value" warning and typelevel-scalafix's "unused IO" warning. This would need to be disabled for side-effecting code of course - so in effectful FP codebases it would have the double benefit of catching unintentionally unused effect values and requiring side-effecting code to be explicitly marked as such.

bplommer avatar Jun 20 '22 15:06 bplommer

Thanks, that sounds like a rule that could benefit many FP codebases indeed! It would be interesting to see the amount of false positives on a project and if/how we could mitigate via some kind of ignore list (so I guess a semantic rule would make sense?).

As for whether it's better to have it as a built-in rule or as a community rule, I don't have a strong opinion. There hasn't been any discussion for years as to where we draw the line between which rules should be built-in vs community.

Although we could improve scalafix-cli, community rules are pretty well supported in scalafix-interfaces now (and not just sbt-scalafix as it used to be), and the scalafix API is quite stable. The reason why we are in the (staled) process of upstreaming the most popular community rule OrganizeImports is mostly due a lack of time from its maintainer. That said, having fresh, active rules being upstreamed will most likely have the good side effect of improving (even so little) the scalafix-core/-rules codebase which has been in maintenance mode for nearly 2 years.

Even though I believe iterating first in a module for which you control the release cycle might be more convenient for you, I'll be happy to review and merge your PR - we can always try it out on several projects using the snapshots) until we cut a release.

bjaglin avatar Jun 27 '22 20:06 bjaglin

Thanks, that sounds like a rule that could benefit many FP codebases indeed! It would be interesting to see the amount of false positives on a project

we can always try it out on several projects using the snapshots) until we cut a release.

I'd be very happy to help out with this process 👍

armanbilge avatar Jun 27 '22 20:06 armanbilge

relevant: https://github.com/scala/scala/pull/9893

SethTisue avatar Jun 30 '22 16:06 SethTisue

By "relevant", is meant, "exactly what you're asking for."

The usual caveats apply: balancing "false positives" against "usability", how many knobs and levers are useful, etc.

Hopefully the built-in -W will be built in soon. There may be other advantages to having a 3rd party lint, despite duplication of work, such as a faster release cycle, better maintenance, etc.

som-snytt avatar Jun 30 '22 16:06 som-snytt

Keeping this open, mostly for Scala 3, as this is now available in the Scala 2.13.9 compiler .

bjaglin avatar Oct 02 '22 20:10 bjaglin

Closing this as the compiler option -Wnonunit-statement is now also available in Scala 3.3.1-RC1.

bjaglin avatar Jun 02 '23 08:06 bjaglin