Tim Oltjenbruns
04/04/2022, 12:56 PMbindingContext.get(BindingContext.EXPRESSION_TYPE_INFO, expression)
So I’m wondering if it may be something specific to how Detekt acquires the BindingContext?gammax
04/04/2022, 1:37 PMTim Oltjenbruns
04/04/2022, 2:21 PMTim Oltjenbruns
04/04/2022, 2:21 PMTim Oltjenbruns
04/04/2022, 2:31 PMbindingContext.get(BindingContext.EXPRESSION_TYPE_INFO, functionExpression)
• bindingContext.get(BindingContext.EXPRESSION_TYPE_INFO, functionExpression.bodyExpression)
• bindingContext.get(BindingContext.EXPECTED_RETURN_TYPE, functionExpression)
• Get the type of the last expression in the block body (unwrapping certain expressions like returns to get the actual type other than Nothing) But then I get the true type, not the type of what’s downcast.
• If there’s a body with no statements then I assume it’s Unit, for my purposes
• If it’s still null then: if its a KtNamedFunction I assume it’s Unit, otherwise it’s returning a class that isn’t in the Detekt classpath (Android SDK). Based on what I found during testing.
It works but I can’t help but think there’s a better way/Tim Oltjenbruns
04/04/2022, 2:36 PMt-kameyama
04/05/2022, 4:08 AMprivate fun KtNamedFunction.returnType(): KotlinType? =
(bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, this] as? FunctionDescriptor)?.returnType
Tim Oltjenbruns
04/05/2022, 12:56 PM