mrmans0n / compose-rules

Lint rules for ktlint/detekt aimed to contribute to a healthier usage of Compose. Actively maintained and evolved fork of the Twitter Compose rules.
https://mrmans0n.github.io/compose-rules
Other
516 stars 18 forks source link

`compose:naming-check` is not honored in subclasses #290

Open Goooler opened 5 days ago

Goooler commented 5 days ago

Describe the bug

Code sample:

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

sealed interface Screen {

  @Suppress("ktlint:compose:naming-check")
  @Composable
  fun content(modifier: Modifier)

  data object Main : Screen {
    @Composable
    override fun content(modifier: Modifier) {}
  }
}

run ktlint check:

Step 'ktlint' found problem in 'src/main/java/Screen.kt':
Error on line: 12, column: 18
rule: compose:naming-check
Composable functions that return Unit should start with an uppercase letter.
They are considered declarative entities that can be either present or absent in a composition and therefore follow the naming rules for classes.

Expected behavior

No warning thrown like in AS

image
mrmans0n commented 4 days ago

There is only so much we could do without type resolution support. It could be done in that specific example, as they are in the same file, but that's not going to be common in most inheritance situations, so I'm hesitant as to whether it's worth doing it at all.

Goooler commented 4 days ago

BaseFoo.kt:

abstract class BaseFoo {

  @Suppress("ktlint:standard:function-naming")
  abstract fun _bar()
}

Foo1.kt:

class Foo1 : BaseFoo() {

  override fun _bar() {
    println("Foo1")
  }
}

The above case works in Ktlint 1.3.1, it should be helpful for fixing this issue. And it also works in Detekt 1.23.6 after replacing the suppress rule to FunctionNaming or FunctionName.