I’ve been doing a bit of Rust recently, and I have to admit I’ve been quite astonished by Clippy, the main linter for the language.
I think having such a linter for OCaml could really be useful to new users as well as to more experienced OCaml developers. As importantly, it would constitute a database of OCaml best practices, which are often quite difficult to assemble.
The ppxlib / compiler-libs that exist and are already quite good would make it much easier than it would have been a few years ago to develop such a linter and I believe its integration would be quite easy in ocaml-lsp/dune. The main pain points would be:
- Defining ocaml best practices (for example, JaneStreet have their own style, the stdlib has a different one…)
- Actually implementing it. That’s one project I would love to take on, but I’m already overwhelmed with work and there are projects I contributed to in the past and haven’t had time to continue to that would probably be a priority for me. I realise this project is a huge task, but I do believe Clippy has an important part to play in beginner’s impression when it comes to Rust.
- Reducing the amount of lint scream when using legacy code which is bound to not follow the best practices. But I believe that’s something that dune could provide easily.
- Version management for the lints (some lints would work in newer versions but not olders).
When I say linter, I don’t say “just” style. OCamlformat is very good and already fixes most of the styling issues that one might have, but clippy also provide guidances such as:
The amount of linting messages in Clippy and variety of it is quite impressive : ALL the Clippy Lints
And quite importantly, once again, a lot of lints are semantic and not stylistic, e.g.:
One example that comes to mind is the fact that I keep finding
List.concat (List.map f x) in some old parts of the code I’m working on, and that could easily be flagged. Some more tricky hints like “This could probably use Seq instead of List” would also be interesting if at all possible.
Before writing this post I’ve looked tried to look for a similar initiative, but haven’t been able to find anything.