简体   繁体   中英

How to enable dead code warnings in Haskell (GHC)

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 (eg -fwarn-unused-binds , -fwarn-name-shadowing , and -fwarn-hi-shadowing ) but none of them seem to accomplish what I want.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM