Haskell: how to tell hlint not to: `Warning: Use string literal`
Asked Answered
A

3

26

I have a unit test file:

module X04PatMatTest where

import AssertError
import Test.HUnit
import X04PatMat

...

and hlint complains:

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.

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 ...

What is the magic?

Aronson answered 8/10, 2013 at 1:43 Comment(0)
E
25

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

Epanorthosis answered 8/10, 2013 at 4:37 Comment(5)
That works - thanks. The HLint documentation is not clear on this. For pragmas on functions it says: {-# ANN myFunction "HLint: ignore" #-} . For pragmas on modules it says: {-# ANN module "HLint: ignore Eta reduce" #-} so it was not clear to me if I should write module or something like myModule.Aronson
@Aronson It is not explicitly stated, that's right. You can somehow derive it from the fact, that the first letter is lowercase and module names start with an uppercase letter. P.S. It would be nice, if you could accept my answer.Epanorthosis
I've updated the docs to read: {-# ANN module "HLint: ignore Eta reduce" #-}</tt> - ignore all eta reduction suggestions in this module (use <tt>module</tt> literally, not the name of the module). They are in git, but the docs link still takes you to the outdated darcs. I've raised a bug to fix that.Sekofski
MoFu : upper case - of course! @neil-mitchell : good to be explicit - thanks.Aronson
Looks like with OverloadedStrings, you may also need an explicit type signature. I had to this just now: {-# ANN validMonetaryChars ("HLint: ignore Use String" :: Text) #-}Vish
A
6

With recent (>2019) hlint, you can use the simpler syntax {- 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.

Ancipital answered 28/6, 2021 at 9:16 Comment(2)
Note that the comment has to be multiline format {- -}, -- doesn't work.Dowry
And you can still assign it to a specific definition (for example a function) so it affects only that part of code, by doing {- HLINT ignore myFunc "Use string literal" -}.Intone
A
2

Agree with @MoFu's solution.

hlint also support ignore specific warning with arguments.

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.

Arborvitae answered 7/11, 2015 at 9:51 Comment(1)
Thanks for this. I'm using hlint in vim with ale and I want to ignore warnings about duplication, so I did something like this: let g:ale_linters = { 'haskell': ['hlint -i "Reduce duplication"'],}.Vish

© 2022 - 2024 — McMap. All rights reserved.