# OCaml warning 20: unused function argument

**URL:** <https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412>\
**Category:** Learning\
**Created:** [March 31, 2020, 8:57am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412 "2020-03-31T08:57:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobzhang](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bobzhang/32/553_2.png) [@bobzhang](https://discuss.ocaml.org/u/bobzhang)\
**Post date:** [March 31, 2020, 8:57am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/1 "2020-03-31T08:57:05Z")

</div>

‘(Obj.magic f) a b’ triggers such warning saying a and b are not used. This warning happens rarely in practice, do you know a use case where such warning catches bugs? Thanks

---

<div class="post-metadata">

**Author:** ![Leonidas](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/leonidas/32/4039_2.png) [@Leonidas](https://discuss.ocaml.org/u/Leonidas)\
**Post date:** [March 31, 2020, 9:01am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/2 "2020-03-31T09:01:22Z")

</div>

You mean in the general case? For me it catches issues all the time, there are two usecases where this proves surprisingly valuable:

1. I meant to do something with a function argument but didn’t. There was logic missing, but of course the type system couldn’t tell me that. Fortunately the warning told me that I forgot to do something with a value I passed it.
2. My logic was wrong and I never needed the argument in the first place, that way I can clean up all my calls (and potentially remove the argument going all the way up the call stack) to avoid it.

---

<div class="post-metadata">

**Author:** ![bobzhang](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bobzhang/32/553_2.png) [@bobzhang](https://discuss.ocaml.org/u/bobzhang)\
**Post date:** [March 31, 2020, 9:16am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/3 "2020-03-31T09:16:06Z")

</div>

Do you have a concrete piece of code that such warning helps? Thanks

Marek Kubica via OCaml [ocaml@discoursemail.com](mailto:ocaml@discoursemail.com)于2020年3月31日 周二下午5:11写道：

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [March 31, 2020, 9:28am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/4 "2020-03-31T09:28:49Z")

</div>

The warning is useful for any function that always raises an exception or diverges?  
For instance with `failwith`:

```ocaml
failwith "A string" ["anything"]

```

or

```ocaml
let loop state = 
  loop (update state)
let _ = loop state more

```

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [March 31, 2020, 9:29am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/5 "2020-03-31T09:29:55Z")

</div>

Easily done:

```auto
let f x = failwith "foo" ;;
f 1 2;;

```

compiling yields:

```auto
5$ ocamlc foo.ml
File "foo.ml", line 4, characters 4-5:
4 | f 1 2;;
        ^
Warning 20: this argument will not be used by the function.

```

This has caught my errors several times.

---

<div class="post-metadata">

**Author:** ![Leonidas](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/leonidas/32/4039_2.png) [@Leonidas](https://discuss.ocaml.org/u/Leonidas)\
**Post date:** [March 31, 2020, 9:33am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/6 "2020-03-31T09:33:46Z")

</div>

Like from my production code? It’s too involved for a post since there is a lot of infrastructure code filled in.

But here’s a transliteration of case 1, the idea of code that we do run in production.

```ocaml
let lookup_username cache user_id =
  let%bind username = lookup_microservice user_id in
  return username

```

Here I forgot to look it up from cache or save it into the cache. The code compiles fine, it does not do the thing it was supposed to do. Now the compiler tells me, that I forgot to actually use cache. Very useful.

I first was annoyed that dune subscribes you to this warning by default and it can be somewhat annoying when debugging, but I’ve come to see it as a big advantage. I think this is also popular in the Golang community, for similar reasons (where ignoring `err` is all too easy).

---

<div class="post-metadata">

**Author:** ![bobzhang](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bobzhang/32/553_2.png) [@bobzhang](https://discuss.ocaml.org/u/bobzhang)\
**Post date:** [March 31, 2020, 9:43am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/7 "2020-03-31T09:43:25Z")

</div>

This seems to be a different warning, unused variable?

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [March 31, 2020, 9:43am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/8 "2020-03-31T09:43:27Z")

</div>

that generates error 27:

```auto
let f x y = print_string y ;;

```

```auto
5$ ocamlc -w +A foo.ml
File "foo.ml", line 2, characters 6-7:
2 | let f x y = print_string y ;;
          ^
Warning 27: unused variable x.

```

---

<div class="post-metadata">

**Author:** ![bobzhang](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bobzhang/32/553_2.png) [@bobzhang](https://discuss.ocaml.org/u/bobzhang)\
**Post date:** [March 31, 2020, 9:43am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/9 "2020-03-31T09:43:54Z")

</div>

Yes, that’s my understanding, thanks for the example

---

<div class="post-metadata">

**Author:** ![bobzhang](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bobzhang/32/553_2.png) [@bobzhang](https://discuss.ocaml.org/u/bobzhang)\
**Post date:** [March 31, 2020, 9:44am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/10 "2020-03-31T09:44:37Z")

</div>

Yeah, I understand warning 27 is pretty common and useful. My question is about warning 20

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [March 31, 2020, 9:46am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/11 "2020-03-31T09:46:37Z")

</div>

Yes, I was replying to @Leonidas, letting them know that their example didn’t generate warning 20, is all. In any case, I have several times written code that triggered this warning, and each time it’s been a bug, waiting to happen.

ETA: triggers this warning in the manner of the example I posted above. That is, a function with multiple args, that can raise an exception after being partially applied.

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [March 31, 2020, 9:47am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/12 "2020-03-31T09:47:03Z")

</div>

If you are generating code with `Obj.magic`, it seems better to add an explicit type constraint  
`(Obj.magic f: _ -> _ -> _) x y` rather than trying to disable the warning.

---

<div class="post-metadata">

**Author:** ![sebbes](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/sebbes/32/1997_2.png) [@sebbes](https://discuss.ocaml.org/u/sebbes)\
**Post date:** [March 31, 2020, 9:52pm UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/13 "2020-03-31T21:52:37Z")

</div>

These kind of warning has been very valuable to me when dealing with some state which was modified along the way in a pure calculation. It wasn’t in OCaml but in elm. The concrete piece of code is here: [https://slides.com/sebbes/compiler-driven-onboarding/#/75](https://slides.com/sebbes/compiler-driven-onboarding/#/75) ; as you can see, it is not obvious at all that I’m doing smthg wrong… This warning enabled me to catch the bug very early in the development process!

(if you want some context, the story starts here: [https://slides.com/sebbes/compiler-driven-onboarding/#/57](https://slides.com/sebbes/compiler-driven-onboarding/#/57) )

---

<div class="post-metadata">

**Author:** ![bobzhang](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bobzhang/32/553_2.png) [@bobzhang](https://discuss.ocaml.org/u/bobzhang)\
**Post date:** [April 1, 2020, 9:34am UTC](https://discuss.ocaml.org/t/ocaml-warning-20-unused-function-argument/5412/14 "2020-04-01T09:34:12Z")

</div>

Hi, this is another warning, used variable
