Jef Claes

On software and life

26 Apr 2015

Finding unused code (F#)

Coming from C#, I’m used to the compiler warning me about unused variables. Relying on the compiler to help me with checked exceptions in F#, I noticed that unused values (and functions) would go unnoticed. Having accidentally read earlier that Haskell has a compiler flag to check for unused bindings, I looked for the F# equivalent but failed to find it, until Scott Wlaschin pointed me in the right direction.

By using the –warnon:1182 flag, the compiler will warn you about unused bindings.

For example, compiling Paket.Core with this flag enabled, outputs the following warnings.

1>C:\Paket\src\Paket.Core\Utils.fs(88,9): warning FS1182: The value 'fi' is unused
1>C:\Paket\src\Paket.Core\Utils.fs(361,46): warning FS1182: The value 'econt' is unused
1>C:\Paket\src\Paket.Core\Utils.fs(361,52): warning FS1182: The value 'ccont' is unused
1>C:\Paket\src\Paket.Core\PackageSources.fs(97,25): warning FS1182: The value 'uri' is unused
1>C:\Paket\src\Paket.Core\RemoteDownload.fs(147,18): warning FS1182: The value 'downloaded' is unused
1>C:\Paket\src\Paket.Core\RemoteUpload.fs(71,16): warning FS1182: The value 'whyIsThisNeeded' is unused
1>C:\Paket\src\Paket.Core\RemoteUpload.fs(85,17): warning FS1182: The value 'progressSubscription' is unused
...

Looking into these warnings revealed values and functions that can be deleted, but no apparent bugs. There are also cases where unused bindings make sense, for example when you pass in a function that does not use all of its arguments or when pattern matching. In these cases you can suppress the warning by prefixing the bindings with an underscore.

fun (cont,_econt,_ccont) -> ...

A useful compiler feature which strangely enough is opt-in. I plan on using it from now on.