scalacenter / scalafix

Refactoring and linting tool for Scala

Home Page:https://scalacenter.github.io/scalafix/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DisableSyntax.noValPatterns could allow unpacking case classes

ashleymercer opened this issue · comments

I'd like to propose allowing unpacking case classes under the DisableSyntax.noValPatterns rule - either always, or possibly with some configuration switch. This is particularly useful for code that has a lot of config objects being passed around:

final case class FooConfig(bar: String, baz: Int)

def doTheThing(conf: FooConfig) = {
  val FooConfig(bar, baz) = conf
  // etc
}

I'm guessing the reason this is disabled in general is because we have no idea what a random unapply method might do, but this should be safe for case classes?

Hi @ashleymercer, thanks for the suggestion!

For the record, the rationale behind that rule is there: #716.

Unfortunately, DisableSyntax is a syntactic rule, so it does not have information about the types, therefore we won't be able to tell whether we are using a case class extractor, let alone the right one matching the right-hand side.

It would be possible to re-implement what noValPatterns protects against with a semantic rule, and include your suggestion to avoid false positives on safe usages. This would however be a whole new rule, so I'd suggest to do that in a custom rule outside the scalafix main repo if you are interested as I don't see a strong case for maintaining that new rule as a built-in one.

Ahh thanks for the link! Interesting reading - and yes totally fair that you don't want to maintain two versions of this rule. I'll go ahead and close this issue.