Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable dead code warnings in Haskell (GHC)

Tags:

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

like image 923
Garrett Bluma Avatar asked Sep 03 '14 16:09

Garrett Bluma


1 Answers

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.

like image 92
GS - Apologise to Monica Avatar answered Oct 22 '22 00:10

GS - Apologise to Monica