I’d like to see a compiler warning for when someth...
# stdlib
m
I’d like to see a compiler warning for when something like
buildString
is used but the
StringBuilder
receiver is never accessed. Same for
buildList
,
buildSet
etc. My motivation for this is that after some refactoring, I made this mistake:
Copy code
buildString {
    items.joinToString(separator = " ") {
        foo(it)
    }
}
k
That sounds fairly reasonable, but too specific to me. How about something more general, such as a compiler warning when the
this
receiver is not used in a lambda? That would include scope functions such as
apply
,
with
and
run
. Maybe this is the wrong channel as it's about compiler behaviour.
m
I think there are often legitimate reasons to not (in general) use a receiver in a lambda, but in the examples I gave, there are no reasons not to use it. Perhaps a function that accepts a lambda can be annotated somehow to say that the receiver must be used.
j
This seems like a similar issue as unused function return values. Sometimes it is completely valid to ignore the value. But other times it should be considered a misuse and result in a warning. Maybe a similar
@CheckReceiver
annotation for functions/lambdas makes sense?
👍 1
k
There are many cases when the receiver is
it
and there's no need to check it, but I can't think of any ignorable cases at the moment for a
this
receiver.
c
A case where it is safe to ignore the receiver:
Copy code
runBlocking {
    delay(5000)
    println("Done")
}
It's reasonable to write this code, and I'm never using the
CoroutineScope
receiver.
👍 1
k
Thanks, and, come to think of it, I do it all the time with KoTest.