1

What's the way to match the following code-pattern...

do 
  x <- createModel a b
  case x of
    Left e -> throwM $ ValidationErrors e
    Right y -> ...

...and suggest the following replacement:

withThrow $ createModel a b

I tried the following, but it doesn't work:

hint: {lhs: "do {x <- createModel v w; case x of Left e -> throwM $ ValidationErrors e}", rhs: "withThrow $ createModel v w"}
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
  • 1
    Even if you succeed, that seems awfully specific. – dfeuer Nov 28 '17 at 20:30
  • 1
    I agree it seems too specific. I think the pattern you're looking for is pattern matching on `Either` with `throwM` in the `Left` branch. Also `withThrow` seems specific too, if it only works for `ValidationErrors`. – Cirdec Nov 28 '17 at 20:37
  • 2
    Separately, I don't think you'll have much luck matching *part* of a case expression. I think you'll at least need to mention the other branch somehow. But I've never written HLint rules, so I'm not sure. – dfeuer Nov 28 '17 at 20:38
  • it's specific because it's only related to our project. Not to be published as a general hlint rule. Let me try matching with Left/Right branches of a case expression. – Saurabh Nanda Nov 29 '17 at 07:25

1 Answers1

1

The problem is that HLint matching is expression based, whereas the rule you're trying to define is really statement based - you want to match the two statements anywhere adjacently in a do. It's possible HLint could be modified to do that, and you think that would be useful, please raise an HLint issue.

Neil Mitchell
  • 9,090
  • 1
  • 27
  • 85