简体   繁体   English

如何在Haskell(GHC)中启用死代码警告

[英]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. 有些语言(如Go和Rust)要求程序员努力从源代码中删除所有死代码。 This has benefits in code maintainability and readability, if a bit extreme for some users. 如果某些用户有点极端,这在代码可维护性和可读性方面具有优势。

How can I enable this feature in Haskell? 如何在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 . (有可能吗?)例如,在下面的代码中,我想将url2标记为死代码,因为它未在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. 我看到了一些编译器标志的引用(例如-fwarn-unused-binds-fwarn-name-shadowing-fwarn-hi-shadowing ),但它们似乎都没有达到我想要的效果。

GHC will report url2 as dead code with -fwarn-unused-binds if you restrict the list of exports from the module appropriately, eg: 如果您适当地限制模块的导出列表,GHC将使用-fwarn-unused-bindsurl2报告为死代码,例如:

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. 然后你隐式导出所有内容,因此它不能认为任何顶级绑定未被使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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