(#) Modifier factory functions should not be @Composable !!! WARNING: Modifier factory functions should not be @Composable This is a warning. Id : `ComposableModifierFactory` Summary : Modifier factory functions should not be @Composable Severity : Warning Category : Correctness Platform : Any Vendor : Jetpack Compose Identifier : androidx.compose.ui Feedback : https://issuetracker.google.com/issues/new?component=612128 Min : Lint 7.0 Compiled : Lint 8.0 and 8.1 Artifact : [androidx.compose.ui:ui](androidx_compose_ui_ui.md.html) Affects : Kotlin and Java files and test sources Editing : This check runs on the fly in the IDE editor Implementation : [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/ui/ui-lint/src/main/java/androidx/compose/ui/lint/ModifierDeclarationDetector.kt) Tests : [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/ui/ui-lint/src/test/java/androidx/compose/ui/lint/ModifierDeclarationDetectorTest.kt) Copyright Year : 2020 Modifier factory functions that need to be aware of the composition should use androidx.compose.ui.composed {} in their implementation instead of being marked as @Composable. This allows Modifiers to be referenced in top level variables and constructed outside of the composition. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text src/androidx/compose/ui/foo/TestModifier.kt:13:Warning: Modifier factory functions should not be marked as @Composable, and should use composed instead [ComposableModifierFactory] fun Modifier.fooModifier1(): Modifier { ------------ src/androidx/compose/ui/foo/TestModifier.kt:19:Warning: Modifier factory functions should not be marked as @Composable, and should use composed instead [ComposableModifierFactory] fun Modifier.fooModifier2(): Modifier = ------------ src/androidx/compose/ui/foo/TestModifier.kt:23:Warning: Modifier factory functions should not be marked as @Composable, and should use composed instead [ComposableModifierFactory] val Modifier.fooModifier3: Modifier get() { ------------ src/androidx/compose/ui/foo/TestModifier.kt:29:Warning: Modifier factory functions should not be marked as @Composable, and should use composed instead [ComposableModifierFactory] val Modifier.fooModifier4: Modifier get() = ------------ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `src/androidx/compose/ui/foo/TestModifier.kt`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers package androidx.compose.ui.foo import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier class TestModifier(val value: Int) : Modifier.Element @Composable fun someComposableCall(int: Int) = 5 @Composable fun Modifier.fooModifier1(): Modifier { val value = someComposableCall(3) return this.then(TestModifier(value)) } @Composable fun Modifier.fooModifier2(): Modifier = this.then(TestModifier(someComposableCall(3))) @get:Composable val Modifier.fooModifier3: Modifier get() { val value = someComposableCall(3) return this.then(TestModifier(value)) } @get:Composable val Modifier.fooModifier4: Modifier get() = this.then(TestModifier(someComposableCall(3))) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also visit the [source code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/ui/ui-lint/src/test/java/androidx/compose/ui/lint/ModifierDeclarationDetectorTest.kt) for the unit tests for this check to see additional scenarios. The above example was automatically extracted from the first unit test found for this lint check, `ModifierDeclarationDetector.composableModifierFactories`. To report a problem with this extracted sample, visit https://issuetracker.google.com/issues/new?component=612128. (##) Including !!! This is not a built-in check. To include it, add the below dependency to your project. ``` // build.gradle.kts implementation("androidx.compose.ui:ui:1.5.0-beta02") // build.gradle implementation 'androidx.compose.ui:ui:1.5.0-beta02' // build.gradle.kts with version catalogs: implementation(libs.ui) # libs.versions.toml [versions] ui = "1.5.0-beta02" [libraries] ui = { module = "androidx.compose.ui:ui", version.ref = "ui" } ``` 1.5.0-beta02 is the version this documentation was generated from; there may be newer versions available. [Additional details about androidx.compose.ui:ui](androidx_compose_ui_ui.md.html). (##) Suppressing You can suppress false positives using one of the following mechanisms: * Using a suppression annotation like this on the enclosing element: ```kt // Kotlin @Suppress("ComposableModifierFactory") fun method() { problematicStatement() } ``` or ```java // Java @SuppressWarnings("ComposableModifierFactory") void method() { problematicStatement(); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection ComposableModifierFactory problematicStatement() ``` * Using a special `lint.xml` file in the source tree which turns off the check in that folder and any sub folder. A simple file might look like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <lint> <issue id="ComposableModifierFactory" severity="ignore" /> </lint> ``` Instead of `ignore` you can also change the severity here, for example from `error` to `warning`. You can find additional documentation on how to filter issues by path, regular expression and so on [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html). * In Gradle projects, using the DSL syntax to configure lint. For example, you can use something like ```gradle lintOptions { disable 'ComposableModifierFactory' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore ComposableModifierFactory ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).