I have a function (`KtNamedFunction`) and I want t...
# detekt
b
I have a function (
KtNamedFunction
) and I want to know if the returning type is Unit or not with type solving. I'm using this:
function.bodyExpression.getResolvedCall(bindingContext)?.resultingDescriptor?.returnType?.isUnit()
but I get null for functions like this:
fun asdf() = 5
. And if I use this:
function.getType(bindingContext)?.fqNameOrNull()?.shortName()?.let { it.toString() == "Unit" }
get
null
for
fun asdf() {}
.
g
My gut feeling tells me the first one is the probably correct one 🤔
t
Copy code
override fun visitNamedFunction(function: KtNamedFunction) {
    val descriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, function] as? FunctionDescriptor
    val isUnit = descriptor?.returnType?.isUnit()
}
❤️ 1
b
Thanks @t-kameyama! It works as a charm!