I’ve run into an interesting issue recently.

The method signature for use is inline fun <T : Closeable?, R> T.use(block: (T) -> R): R

Because it works on Closeable?, there are two null-checks required. This adds unnecessary instructions to the byte code, and causes FindBugs to warn me.

I’m not sure I understand why you’d want the block to run if the Closeable was null, versus doing something like closeable?.use { ... }.

I considered adding my own inline extension function that requires a Closeable, but then that needs to be maintained.

Am I missing a better option here? Would it be reasonable for the compiler to see that a non-null type is being passed in, and turn the two this?.close() calls into this.close()?

Allowing nullable receiver for use extension covers two use cases, see this issue for details:

What warnings does it cause with FindBugs?

Oops, you mean the code block will be invoked even if resource is null? It is a little counterintuitive to me. “use” but not “used”

Note that it is a generic parameter and if you don’t want to allow null in the block you don’t have to (and can avoid it by using closeable?.use{}

Redundant nullcheck of value known to be non-null

I suppose at some point the inliner will be able to eliminate branches of the inlined code that it can prove unreachable.
There’s a similar feature request for that