Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning unused variable in Haskell code

Tags:

haskell

Real World Haskell says "Haskell compiler can warn us if we introduce a variable name in a pattern, but do not use it in a function's body".

I often forget to use variables which I bind, so I want to use this feature. But my ghci and ghc seem to be fine with unused variables, and I actually have never seen this since I started learning Haskell a few months ago.

How can I use this feature? Or Haskell compiler does not have this feature anymore?

like image 414
Tengu Avatar asked Jun 15 '13 15:06

Tengu


3 Answers

As said before, the option is -fwarn-unused-binds or -Wall for all warnings. Another one is -Werror to make the warnings stop the compilation.

To use them, you pass them to ghc or ghci on the command-line. Additionally, during a session in ghci, you can type

:set -fwarn-unused-binds

With cabal you can add ghc-options to the executable section. For instance :

executable my-program
  main-is:        MyProgram.hs
  hs-source-dirs: src
  ghc-options:    -Wall

Now, running cabal configure && cabal build (and other commands) will use the -Wall option.

like image 175
tomferon Avatar answered Nov 02 '22 13:11

tomferon


You can look at Warnings. I generally use -Wall in my projects which shows unused binded variables along with several other useful warnings. There is specific flag (-fwarn-unused-binds) too to do just what you want.

like image 6
Satvik Avatar answered Nov 02 '22 13:11

Satvik


You can add the -fwarn-unused-binds flag to ghc or ghci. To enable other useful warnings, use -W. To enable all warnings, use -Wall. For example:

ghci -fwarn-unused-binds
like image 5
dflemstr Avatar answered Nov 02 '22 12:11

dflemstr