(#) Transition.animate* calls should use the provided targetState when defining values !!! ERROR: Transition.animate* calls should use the provided targetState when defining values This is an error. Id : `UnusedTransitionTargetStateParameter` Summary : Transition.animate* calls should use the provided targetState when defining values Severity : Error Category : Correctness Platform : Any Vendor : Jetpack Compose Identifier : androidx.compose.animation.core Feedback : https://issuetracker.google.com/issues/new?component=612128 Min : Lint 8.0 and 8.1 Compiled : Lint 8.7+ Artifact : [androidx.compose.animation:animation-core-android](androidx_compose_animation_animation-core-android.md.html) Since : 1.5.0 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/animation/animation-core-lint/src/main/java/androidx/compose/animation/core/lint/TransitionDetector.kt) Tests : [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/animation/animation-core-lint/src/test/java/androidx/compose/animation/core/lint/TransitionDetectorTest.kt) Copyright Year : 2021 Transition.animate* functions provide a target state parameter in the lambda that will be used to calculate the value for a given state. This target state parameter in the lambda may or may not be the same as the actual state, as the animation system occasionally needs to look up target values for other states to do proper seeking/tooling preview. Relying on other state than the provided `targetState` could also result in unnecessary recompositions. Therefore, it is generally considered an error if this `targetState` parameter is not used. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text src/foo/test.kt:13:Error: Target state parameter it is not used [UnusedTransitionTargetStateParameter] transition.animateFloat { if (foo) 1f else 0f } ----------------------- src/foo/test.kt:14:Error: Target state parameter it is not used [UnusedTransitionTargetStateParameter] transition.animateFloat(targetValueByState = { if (foo) 1f else 0f }) ----------------------- src/foo/test.kt:15:Error: Target state parameter param is not used [UnusedTransitionTargetStateParameter] transition.animateFloat { param -> if (foo) 1f else 0f } ----- src/foo/test.kt:16:Error: Target state parameter param is not used [UnusedTransitionTargetStateParameter] transition.animateFloat(targetValueByState = { param -> if (foo) 1f else 0f }) ----- src/foo/test.kt:17:Error: Target state parameter _ is not used [UnusedTransitionTargetStateParameter] transition.animateFloat { _ -> if (foo) 1f else 0f } - src/foo/test.kt:18:Error: Target state parameter _ is not used [UnusedTransitionTargetStateParameter] transition.animateFloat(targetValueByState = { _ -> if (foo) 1f else 0f }) - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `src/foo/test.kt`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers package foo import androidx.compose.animation.core.* import androidx.compose.runtime.* val transition = Transition() var foo = false @Composable fun Test() { transition.animateFloat { if (foo) 1f else 0f } transition.animateFloat(targetValueByState = { if (foo) 1f else 0f }) transition.animateFloat { param -> if (foo) 1f else 0f } transition.animateFloat(targetValueByState = { param -> if (foo) 1f else 0f }) transition.animateFloat { _ -> if (foo) 1f else 0f } transition.animateFloat(targetValueByState = { _ -> if (foo) 1f else 0f }) } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also visit the [source code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/animation/animation-core-lint/src/test/java/androidx/compose/animation/core/lint/TransitionDetectorTest.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, `TransitionDetector.unreferencedParameters`. 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.animation:animation-core-android:1.8.0-alpha07") // build.gradle implementation 'androidx.compose.animation:animation-core-android:1.8.0-alpha07' // build.gradle.kts with version catalogs: implementation(libs.animation.core.android) # libs.versions.toml [versions] animation-core-android = "1.8.0-alpha07" [libraries] # For clarity and text wrapping purposes the following declaration is # shown split up across lines, but in TOML it needs to be on a single # line (see https://github.com/toml-lang/toml/issues/516) so adjust # when pasting into libs.versions.toml: animation-core-android = { module = "androidx.compose.animation:animation-core-android", version.ref = "animation-core-android" } ``` 1.8.0-alpha07 is the version this documentation was generated from; there may be newer versions available. NOTE: These lint checks are **also** made available separate from the main library. You can also use `androidx.compose.animation:animation-core-lint:1.8.0-alpha07`. [Additional details about androidx.compose.animation:animation-core-android](androidx_compose_animation_animation-core-android.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("UnusedTransitionTargetStateParameter") fun method() { problematicStatement() } ``` or ```java // Java @SuppressWarnings("UnusedTransitionTargetStateParameter") void method() { problematicStatement(); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection UnusedTransitionTargetStateParameter 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="UnusedTransitionTargetStateParameter" 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 'UnusedTransitionTargetStateParameter' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore UnusedTransitionTargetStateParameter ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).