elm-analyse icon indicating copy to clipboard operation
elm-analyse copied to clipboard

Add support for phantom types for unused constructor value check

Open akoppela opened this issue 6 years ago • 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?

akoppela avatar Jan 28 '19 10:01 akoppela

I'm encountering this problem as well!

Augustin82 avatar Dec 13 '19 09:12 Augustin82

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

hanneskaeufler avatar Feb 02 '20 11:02 hanneskaeufler

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

decioferreira avatar Feb 28 '20 15:02 decioferreira

@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 avatar May 03 '20 08:05 sparksp

@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.

decioferreira avatar May 04 '20 08:05 decioferreira

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.

jfmengels avatar May 04 '20 08:05 jfmengels