Unwarned error between ignored-partial-application and unused-var

I spent a few hours today debugging a blindingly obvious ignored partial application bug. We would quite like to avoid this in the future and catch it statically. I’m seeking advice on how to catch this with existing warnings and if not seeking opinions on whether it would be a good addition as a new/extended warning?

In our codebase, we rely on unused-var and ignored-partial-application being errors to catch programming mistakes but the following falls through the cracks:

let () =
  let f a b = a + b in
  let b = f 42 in (* unused-var warning *)
  let _ = f 42 in (* ignored-partial-application warning *)
  let _a = f 42 in (* neither of the above *)
  ()

_a prevents the ignored partial application to be involved as it is a legitimate binding that can be used later. However, because it commonly indicates an ignored variable, it doesn’t trip up unused-var. So us using _a to be a more descriptive _ prevents us from catching the ignored partial application.

If we could force ignored let bindings to be type annotated, we could also catch this bug. But I don’t think there is a warning to that effect either.

In an ideal world, I’d extend ignored-partial-application to catch this case, this would be a one liner implementation, but it fails even in the OCaml codebase. If that is not an option, would introducing a new ignored-partial-application-strict be a better alternative? Or is there some other warning I can use to catch this?

1 Like

Not exactly what you asked, but personally when I wish to explicitly describe a result that is being ignored, I typically use a type annotation instead: let _ : ty = f 42. If the function f changes and f 42 becomes a partial application, this annotation typically catches the issue. Note also that ty does not need to be fully written out, as you can use wildcards for complicated parts, eg: _ list.

Cheers,
Nicolas

I’d be very happy with a warning that forces us to put a type annotation when the let binding is ignored. But I don’t think there’s a warning to that effect either, right?

The goal is to make sure that others don’t make the mistake in the future and to statically enforce it.

No, there isn’t. I suggest you open an issue over at Issues · ocaml/ocaml · GitHub to discuss this issue.

Cheers,
Nicolas

1 Like