我可以让 -Wincomplete-patterns 更严格吗?

Can I make -Wincomplete-patterns more strict?

使用 GHC,我可以启用 -Wincomplete-patterns 来捕捉像这两种可能的错误情况。

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

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

显然,我忘记处理这两个函数中的 Right 情况,GHC 会告诉我这一点。然而,编译器似乎在这两种情况下甚至没有警告就让我离开了。

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

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

我还是忘记办案了,不过GHC似乎并不在意。有没有我可以设置的编译器标志来捕获像这样的情况,我使用 let 或 lambda 模式匹配但没有处理所有情况?理想情况下,如果我做这样的事情,我希望得到警告,这样我就可以将其重构为 case 语句等。

当然,为了完整性和后代,也非常感谢与其他编译器相关的答案。

看来-Wincomplete-uni-patterns正是您所需要的。作为基本上一直使用 -Wall 的人,我发现 -Wall-Wincomplete-patterns 未涵盖这些情况令人惊讶和糟糕。

编辑:似乎接受了将此添加到 -Wall 的 GHC 提案。我不确定状态(我检查了 8.4):https://github.com/ghc-proposals/ghc-proposals/pull/71