[英]Haskell: how to tell hlint not to: `Warning: Use string literal`
I have a unit test file:我有一个单元测试文件:
module X04PatMatTest where
import AssertError
import Test.HUnit
import X04PatMat
...
and hlint complains:和 hlint 抱怨:
X04PatMatTest.hs:15:69: Warning: Use string literal
Found:
['a', 'b', 'd']
Why not:
"abd"
for various reasons, I really want to put ['a', 'b', 'd']
in the test code.出于各种原因,我真的很想把
['a', 'b', 'd']
放在测试代码中。
I have tried various permuatations of我已经尝试了各种排列
{-# ANN X04PatMatTest "HLint: ignore Warning: Use string literal" #-}
like putting the pragma as the first line of the file, after the module declaration, with the name module
instead of X04...
, changing the Warning
to warn
...就像将编译指示作为文件的第一行,在模块声明之后,使用名称
module
而不是X04...
,将Warning
更改为warn
...
What is the magic?什么是魔法?
You need to write the pragma in another way.您需要以另一种方式编写编译指示。 After some trial and error I came up with the following:
经过一些试验和错误,我想出了以下内容:
module Test where
import Data.Char(toUpper)
{-# ANN module "HLint: ignore Use string literal" #-}
main :: IO ()
main = putStrLn ['a','b','c']
note that you have to write "module"
and not the name of the module请注意,您必须写
"module"
而不是"module"
的名称
Agree with @MoFu's solution.同意@MoFu 的解决方案。
hlint also support ignore specific warning with arguments. hlint 还支持忽略带有参数的特定警告。
hlint -i 'Use string literal' [filename]
add this to arguments or aliases, so ignore this warning, but not break your code.将此添加到参数或别名中,因此请忽略此警告,但不要破坏您的代码。
By the way, synatastic support arguments.顺便说一下,synatastic 支持论点。
With recent (>2019) hlint, you can use the simpler syntax {- HLINT ignore "some hint" -}
:使用最近的 (>2019) hlint,您可以使用更简单的语法
{- HLINT ignore "some hint" -}
:
module Test where
import Data.Char(toUpper)
{- HLINT ignore "Use string literal" -}
main :: IO ()
main = putStrLn ['a','b','c']
Unlike ANN
, this can be placed anywhere in the file, and there's no need for annotating it as String
if you use OverloadedStrings
, and it will not lead to increased compile time.与
ANN
不同,它可以放置在文件中的任何位置,如果使用OverloadedStrings
则不需要将其注释为String
,并且不会导致编译时间增加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.