stil4m/elm-analyse

Add support for phantom types for unused constructor value check

Opened this issue · 6 comments

At the moment if a custom type is used as a phantom type (only in type annotations) then unused constructor value check fails. It would be nice to allow phantom types. E.g. if a custom type has only one constructor and is used in type annotation then just let it be. Thoughts?

I'm encountering this problem as well!

Is there a workaround for this? I don't see anything in the docs to disable a rule on a per-line basis for example?

The type where it complains is:

type Seconds
    = Seconds

I found a workaround for this by exposing the constructor Seconds(..) on the file

@decioferreira the problem with exposing the constructor for a phantom type is that you don't want phantom types to be constructed. It shouldn't harm anything for someone to make a Seconds, but it feels dirty to give them the chance.

@sparksp sorry, that is not what I meant by "workaround". I agree that this issue still needs solving, I was just sharing a way I found to allow for elm-analyse to still be enabled everywhere on my codebase and not fail on the phantom types. Sorry if my previous comment was not clear enough.

I solved this problem in the equivalent elm-review rule by having the rule/check look for whether the type is used in the stead of a type variable that does not appear in any of the custom type's constructors. So it is pretty much what @akoppela suggested but a tiny bit more elabored.

It knows about the custom types declared in other modules, so it has no problems detecting unused type variables for imported types.

For opaque types from dependencies though, like the types from elm-units, the rule won't be able to tell that, so I made the rule configurable so that users can tell which types (variables) will be phantom types. (further and more detailed explanations in the link)

My suggestion is to do the same checks, and have the check be configurable to handle the case where it is lacking in information.