1 /*
2  * Copyright 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.lifecycle.runtime.compose.lint
18 
19 import androidx.compose.lint.Name
20 import androidx.compose.lint.Package
21 import androidx.compose.lint.inheritsFrom
22 import androidx.compose.lint.isInvokedWithinComposable
23 import com.android.tools.lint.client.api.UElementHandler
24 import com.android.tools.lint.detector.api.Category
25 import com.android.tools.lint.detector.api.Detector
26 import com.android.tools.lint.detector.api.Implementation
27 import com.android.tools.lint.detector.api.Issue
28 import com.android.tools.lint.detector.api.JavaContext
29 import com.android.tools.lint.detector.api.Scope
30 import com.android.tools.lint.detector.api.Severity
31 import com.android.tools.lint.detector.api.SourceCodeScanner
32 import com.intellij.psi.PsiMethod
33 import java.util.EnumSet
34 import org.jetbrains.uast.USimpleNameReferenceExpression
35 import org.jetbrains.uast.tryResolve
36 
37 /**
38  * [com.android.tools.lint.detector.api.Detector] that checks calls to Lifecycle.currentState to
39  * make sure they don't happen inside the body of a composable function / lambda.
40  *
41  * Based on [androidx.compose.runtime.lint.ComposableStateFlowValueDetector].
42  */
43 class ComposableLifecycleCurrentStateDetector : Detector(), SourceCodeScanner {
getApplicableUastTypesnull44     override fun getApplicableUastTypes() = listOf(USimpleNameReferenceExpression::class.java)
45 
46     override fun createUastHandler(context: JavaContext) =
47         object : UElementHandler() {
48             override fun visitSimpleNameReferenceExpression(node: USimpleNameReferenceExpression) {
49                 // Look for a call to .currentState that comes from Lifecycle
50                 if (node.identifier != "currentState") return
51                 val method = node.tryResolve() as? PsiMethod ?: return
52                 if (method.containingClass?.inheritsFrom(LifecycleName) == true) {
53                     if (node.isInvokedWithinComposable()) {
54                         context.report(
55                             LifecycleCurrentStateInComposition,
56                             node,
57                             context.getNameLocation(node),
58                             "Lifecycle.currentState should not be called within composition",
59                             fix()
60                                 .replace()
61                                 .text("currentState")
62                                 .with("currentStateAsState().value")
63                                 .imports("androidx.lifecycle.compose.currentStateAsState")
64                                 .build()
65                         )
66                     }
67                 }
68             }
69         }
70 
71     companion object {
72         val LifecycleCurrentStateInComposition =
73             Issue.Companion.create(
74                 "LifecycleCurrentStateInComposition",
75                 "Lifecycle.currentState should not be called within composition",
76                 "Calling Lifecycle.currentState within composition will not observe changes to the " +
77                     "Lifecycle, so changes might not be reflected within the composition. Instead " +
78                     "you should use lifecycle.currentStateAsState() to observe changes to the Lifecycle, " +
79                     "and recompose when it changes.",
80                 Category.Companion.CORRECTNESS,
81                 3,
82                 Severity.ERROR,
83                 Implementation(
84                     ComposableLifecycleCurrentStateDetector::class.java,
85                     EnumSet.of(Scope.JAVA_FILE, Scope.TEST_SOURCES)
86                 )
87             )
88     }
89 }
90 
91 private val LifecyclePackageName = Package("androidx.lifecycle")
92 private val LifecycleName = Name(LifecyclePackageName, "Lifecycle")
93