Jetpack Compose false positive for function name capitalization

We’re seeing false positives for Jetpack Compose function names that are annotated with Composable, in which the convention is to capitalize the first letter of the Kotlin function:

@Composable
fun SampleScreen() { ... }

The false positive is:
Rename function "SampleScreen" to match the regular expression ^[a-z][a-zA-Z0-9]*$

This contradicts the style guide for Compose here:

which states:
“Jetpack Compose framework development and Library development MUST name any function that returns Unit and bears the @Composable annotation using PascalCase, and the name MUST be that of a noun, not a verb or verb phrase, nor a nouned preposition, adjective or adverb. Nouns MAY be prefixed by descriptive adjectives. This guideline applies whether the function emits UI elements or not. App development SHOULD follow this same convention.”

Hello @kurteous, welcome to the Sonar Community, and thanks for bringing to light this special case to cover in rule S100.

The issue [SONARKT-374] - Jira has been created.

Cheers,
Angelo

Hello @kurteous,

I looked at this ticket, and I think the convention you mention applies to any factory function, not just the @Composable functions. See code convention.

I remember we’ve already discussed this point in the team long time ago, and we changed the default to allow PaskalCase. See here: sonar-kotlin/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/BadFunctionNameCheck.kt at ec549544a971fbfdee90b0deb366a678265851ac · SonarSource/sonar-kotlin · GitHub

I could think of 2 reasons why you still reference an old regex:

  • You use a pretty old version of SQ (<9.9) which has a very old Kotlin analyzer and doesn’t contain this change
  • You copied a SonarWay quality profile a long time ago before the default parameter was changed, and you never updated it.

As for the resolution, I think I can close the ticket, as the default already works the way code convention requires and according to Compose spec; from your side, since the rule is configurable, just change the parameter to the regex you want. Probably "^[a-zA-Z][a-zA-Z0-9]*$" should work.

Best,
Margarita