hlint icon indicating copy to clipboard operation
hlint copied to clipboard

Is it possible to ignore restricted function for a particular function?

Open newhoggy opened this issue 5 years ago • 6 comments

I'm thinking something like this:

{- HLINT ignore myShow "Avoid restricted function: show" -}
myShow :: Show a => a -> String
myShow = show

newhoggy avatar Jul 20 '20 09:07 newhoggy

{- HLINT ignore issue1081 "Avoid restricted function" -}
issue1081 = unsafePerformIO

That works for me, and I have a warning about disable unsafePerformIO.

Would be great if things like the Haskell IDE had a mode which let you easily ignore a hint.

ndmitchell avatar Jul 28 '20 16:07 ndmitchell

It would be great if we could do this for specific restricted functions rather than turning off the Avoid restricted function warning in general. In my use case, I have an HLint warning configured for putStrLn that tells developers to prefer a specific structured logging function instead of printing directly. However:

  • I want my CLI tools (as opposed to my server program) to be able to use putStrLn.
  • All of my separate CLI tools' entry modules are called Main, so I can't use within: to allow specific modules.
  • I don't want my CLI tools to be able to import other restricted functions (e.g. head).

I'm working around this right now by implementing the lint as a regular hint using some nonsense rhs, and then ignoreing that hint by name.

elldritch avatar Aug 18 '22 07:08 elldritch

That seems pretty reasonable @elldritch. One option would be to allow a name field on the restricted function, then you can specify which restricted version you want to ignore. The other way would be to make the labels somehow hierarchical, so match the Avoid restricted function part alone, or more specific. The final option would be to make these ignore pragmas be substring matches, and then you can add more specific things at the end without breaking the matching. My inclination is to do the substring matches, but curious what other people think.

ndmitchell avatar Aug 21 '22 09:08 ndmitchell

These all sound like reasonable options to me.

How exactly would the substring match semantics work? Today, lint warnings look like:

file.hs:1:2-3: Warning: Avoid restricted function
Found:
  someSymbol
Note: This is the message of the lint rule

In this case, what value would be substring matched against?

Would it be the warning + the note? Would it include the "\nNote:" bit? That's a bit of a departure from "the hint's name is its display string", which is a design I actually really like because it's very easy to explain to new users.

elldritch avatar Aug 22 '22 17:08 elldritch

Currently the hint name is "Avoid restricted function". The suggestion would be to make the hint name "Avoid restricted function: someSymbol" and the ignore would have to be a substring of that - so "restricted", "Avoid restricted function" and "function: someSymbol" would all work. I agree the name being the display string is a simple and effective design, so no desire to move away from that.

ndmitchell avatar Aug 22 '22 20:08 ndmitchell

Yeah, that sounds great.

elldritch avatar Aug 23 '22 19:08 elldritch