kowainik / stan

🕵️ Haskell STatic ANalyser
https://kowainik.github.io/projects/stan
Mozilla Public License 2.0
565 stars 48 forks source link

STAN-0213 doesn't know single-constructor data aren't sums #351

Closed hseg closed 4 years ago

hseg commented 4 years ago

Consider data R = R { f1 :: Int, f2 :: T2 }. Clearly, a pattern match of form

\case of
(R 0 t) -> ...
_ -> ...

isn't going to fail. However, STAN-0213 still fires.

chshersh commented 4 years ago

STAN-0213 is not about pattern-matching failures. It is about maintainability issue, e.g. when you add a new constructor to data type, you won't automatically know what functions you need to update since GHC won't warn you if you use _. In your case, if you add a new constructor to the R type, your function can potentially become broken if you forget to handle other constructors.

In this particular situation, you should apply one of the following solutions to make your code more robust against future changes:

\case of
    (R 0 t) -> ...
    R{} -> ...

\case of
    (R 0 t) -> ...
    _don'tCare -> ...
hseg commented 4 years ago

Fair enough.