appKODE / detekt-rules-compose

A collection of Detekt rules for Jetpack Compose
MIT License
136 stars 8 forks source link

`UnnecessaryEventHandlerParameter` raises an exception #14

Closed BraisGabin closed 2 years ago

BraisGabin commented 2 years ago

I wanted to Suppress an issue raised by UnnecessaryEventHandlerParameter because it is a false positive (more about this in #13)

But when I add the @Suppress to the parameter like this:

@Composable
internal fun MyButton(
    state: State,
    @Suppress("UnnecessaryEventHandlerParameter") onClick: (String) -> Unit,
) {
    when (state) {
        State.Loading -> Text("Loading")
        is State.Data -> Button(onClick = { onClick(state.id) }) { Text("Click here") }
    }
}

I'm getting this exception:

> Analyzing /Users/brais.gabin/Workspace/lidl/features/selfscanning/src/main/java/es/lidlplus/features/selfscanning/checkout/AA.kt led to an exception.
  Location: ru.kode.detekt.rule.compose.UnnecessaryEventHandlerParameter$UnnecessaryHandlerArgumentsVisitor.reportError(UnnecessaryEventHandlerParameter.kt:114)
  The original exception message was: class org.jetbrains.kotlin.psi.KtDeclarationModifierList cannot be cast to class org.jetbrains.kotlin.psi.KtTypeReference (org.jetbrains.kotlin.psi.KtDeclarationModifierList and org.jetbrains.kotlin.psi.KtTypeReference are in unnamed module of loader java.net.URLClassLoader @54724ee9)
  Running detekt '1.21.0-RC2' on Java '11.0.11+9' on OS 'Mac OS X'

The interesting part of the stack trace is this:

Caused by: java.lang.ClassCastException: class org.jetbrains.kotlin.psi.KtDeclarationModifierList cannot be cast to class org.jetbrains.kotlin.psi.KtTypeReference (org.jetbrains.kotlin.psi.KtDeclarationModifierList and org.jetbrains.kotlin.psi.KtTypeReference are in unnamed module of loader java.net.URLClassLoader @54724ee9)
        at ru.kode.detekt.rule.compose.UnnecessaryEventHandlerParameter$UnnecessaryHandlerArgumentsVisitor.reportError(UnnecessaryEventHandlerParameter.kt:114)
        at ru.kode.detekt.rule.compose.UnnecessaryEventHandlerParameter$UnnecessaryHandlerArgumentsVisitor.visitCallExpression(UnnecessaryEventHandlerParameter.kt:107)

If I set the suppress at the function level all works as expected.

dimsuz commented 2 years ago

Looks like I was overly specific/optimistic about psi structure somewhere. I will take a look!