本文介绍了如何在Haskell中启用死代码警告(GHC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些语言(如Go& Rust)要求程序员勤奋地从源代码中移除所有的死代码。这对代码的可维护性和可读性有好处,如果对于某些用户有点极端。



如何在Haskell中启用此功能? (可能吗?)例如,在下面的代码中,我想将

如果您限制,GHC会将 url2 报告为死码 -fwarn-unused-binds 例如:

 模块Main(main)其中
...

如果你的模块头只是

 模块Main其中

然后您隐式地导出所有内容, t con sider任何顶级绑定将被使用。


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.

解决方案

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.

这篇关于如何在Haskell中启用死代码警告(GHC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 16:15