Can HLint automatically do suggested edits?

585 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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.

0
On

To complement Zeta's answer, I use some additional flags to perform the refactoring in place:

hlint  --refactor --refactor-options="--inplace" path/to/your/File.hs

Sometimes you only want to apply a certain kind of hint, which can be done by passing the -o flag, e.g.:

hlint  -o="Use fewer imports" --refactor --refactor-options="--inplace" 

As far as I know, the suggested refactorings need to be done in a per-file basis.