How to enable dead code warnings in Haskell (GHC)

喜欢而已 提交于 2019-12-08 16:57:08

问题


Some languages (like Go & Rust) require the programmer to be diligent in removing all dead code from the source. This has benefits in code maintainability and readability, if a bit extreme for some users.

How can I enable this feature in Haskell? (Is it possible?) For example, in the following code, I'd like url2 to be flagged as dead code because it isn't used in main.

url1 = "http://stackoverflow.com"
url2 = "http://stackexchange.com"

main = print url1

I saw reference to some compiler flags (e.g. -fwarn-unused-binds, -fwarn-name-shadowing, and -fwarn-hi-shadowing) but none of them seem to accomplish what I want.


回答1:


GHC will report url2 as dead code with -fwarn-unused-binds if you restrict the list of exports from the module appropriately, e.g.:

module Main(main) where
...

If your module header is just

module Main where

then you are implicitly exporting everything and so it can't consider any top-level binding to be unused.



来源:https://stackoverflow.com/questions/25649143/how-to-enable-dead-code-warnings-in-haskell-ghc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!