Search code examples
haskellrefactoringautomated-refactoringhlint

Can HLint automatically do suggested edits?


HLint gives suggestion how one can improve source code. However, given the nature of the suggestions, I wonder whether it's possible to automatically apply those suggestions.

Is it possible to automatically apply suggestions proposed by hlint?


Solution

  • You have to use --refactor and have the refactor executable in your $PATH. See hlint's README for more information:

    Automatically Applying Hints

    By supplying the --refactor flag hlint can automatically apply most suggestions. Instead of a list of hints, hlint will instead output the refactored file on stdout. In order to do this, it is necessary to have the refactor executable on you path. refactor is provided by the apply-refact package, it uses the GHC API in order to transform source files given a list of refactorings to apply. Hlint directly calls the executable to apply the suggestions.

    Note that hlint doesn't apply transformations recursively, so additional --refactor could be nec­es­sary. That being said, make sure to commit/save before you apply hlint --refactor and test your code afterwards, since the changes might break your code, especially if you use Rank2Types or seq.