Is there a linter that points out nested conditionals?

‘Dangling else’ is a known problem in OCaml syntax; is there a linter that checks for it?

I know this is not an answer to your question; but if this becomes a problem, I think it’s a strong sign that you’re writing bad code. (I’d be happy to be proven wrong with a counter example.)

The reason I’m making that point is because this means this sort of bug can be easily ruled out simply by not writing that kind of code.

I don’t know whether this is a strong sign that you’re writing bad code.

If that’s the case, lots of folks have been writing bad code for some time, as this has been discussed at length in this issue and all the other linked issues and PRs.

2 Likes

I’d be curious to see some concrete examples of this problem to understand this better.

Here’s one: How does one pattern match two list exhaustively?

Another one: Tail cascade: a new indentation style for some OCaml constructs

Right, but that example is also bad code (both lists should be matched on together).

There are definitely cases where nested matches are the right thing to do; in that case I would always use begin … end to avoid confusion.

The proposal in the second link is admittedly kind of neat, though, and drops begin … end.

“no true scotsman”? What do you call bad code in this context?

It’s been a problem in my code for years, and I’ve adopted a defensive style where I almost always wrap if-bodies with ( ) as if they were { } in other languages. I still think that’s a wart and a footgun of the language, not the fault of users.

3 Likes

Yes, that’s why I am asking if there is a linter that enforces this good practice.

Found one: https://www.typerex.org/ocp-lint/#plugin_parsing__raw_syntax__2__warn