Can I make -Wincomplete-patterns more strict?

Viewed 92

With GHC, I can enable -Wincomplete-patterns to catch likely erroneous situations like these two.

problem1 :: Either Int String -> Int
problem1 (Left x) = x

problem2 :: Either Int String -> Int
problem2 x = case x of
               Left x' -> x'

Clearly, I've forgotten to handle the Right case in both of these functions, and GHC will tell me that. However, the compiler seems to let me off without even a warning in these two cases.

problem3 :: Either Int String -> Int
problem3 x = let Left x' = x in x'

problem4 :: Either Int String -> Int
problem4 = \(Left x) -> x

I still forgot to handle a case, but GHC doesn't seem bothered. Is there a compiler flag I can set to catch situations like this where I use let or lambda pattern matching but didn't handle all of the cases? Ideally, I want to be warned if I do something like this so I can refactor it into a case statement or the like.

Of course, for the sake of completeness and posterity, answers relevant to other compilers are highly appreciated as well.

1 Answers

It seems that -Wincomplete-uni-patterns is what you need. As someone who uses -Wall basically all the time, I find the fact that those cases aren't covered by -Wall or -Wincomplete-patterns surprising and bad.

EDIT: It appears a GHC proposal to add this to -Wall was accepted. I'm not sure the status (I checked on 8.4): https://github.com/ghc-proposals/ghc-proposals/pull/71

Related