Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d6c34259 authored by Peter Kalauskas's avatar Peter Kalauskas Committed by Android (Google) Code Review
Browse files

Merge "New lint detector for flow usage" into main

parents 227f7ca1 de4b6e18
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -191,6 +191,7 @@ android_library {
    lint: {
        extra_check_modules: ["SystemUILintChecker"],
        warning_checks: ["MissingApacheLicenseDetector"],
        baseline_filename: "lint-baseline.xml",
    },
}

+1 −0
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ java_test_host {
    data: [
        ":framework",
        ":androidx.annotation_annotation-nodeps",
        ":kotlinx-coroutines-core",
    ],
    static_libs: [
        "SystemUILintChecker",
+112 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.android.internal.systemui.lint

import com.android.tools.lint.detector.api.Category
import com.android.tools.lint.detector.api.Detector
import com.android.tools.lint.detector.api.Implementation
import com.android.tools.lint.detector.api.Issue
import com.android.tools.lint.detector.api.JavaContext
import com.android.tools.lint.detector.api.Scope
import com.android.tools.lint.detector.api.Severity
import com.android.tools.lint.detector.api.SourceCodeScanner
import com.intellij.psi.PsiClassType
import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiParameter
import org.jetbrains.kotlin.psi.psiUtil.isTopLevelKtOrJavaMember
import org.jetbrains.uast.UCallExpression

/** Detects bad usages of Kotlin Flows */
class FlowDetector : Detector(), SourceCodeScanner {

    override fun getApplicableMethodNames(): List<String> =
        listOf(
            FUN_MUTABLE_SHARED_FLOW,
            FUN_SHARE_IN,
        )

    override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
        if (
            method.name == FUN_MUTABLE_SHARED_FLOW &&
                method.parent.isTopLevelKtOrJavaMember() &&
                context.evaluator.getPackage(method)?.qualifiedName == PACKAGE_FLOW
        ) {
            context.report(
                issue = SHARED_FLOW_CREATION,
                location = context.getNameLocation(node),
                message =
                    "`MutableSharedFlow()` creates a new shared flow, which has poor performance " +
                        "characteristics"
            )
        }
        if (
            method.name == FUN_SHARE_IN &&
                getTypeOfExtensionMethod(method)?.resolve()?.qualifiedName == CLASS_FLOW
        ) {
            context.report(
                issue = SHARED_FLOW_CREATION,
                location = context.getNameLocation(node),
                message =
                    "`shareIn()` creates a new shared flow, which has poor performance " +
                        "characteristics"
            )
        }
    }

    companion object {
        @JvmStatic
        val SHARED_FLOW_CREATION =
            Issue.create(
                id = "SharedFlowCreation",
                briefDescription = "Shared flow creation",
                explanation =
                    """
                            Shared flows scale poorly with the number of collectors in use due to
                            their internal buffering mechanism and reliance on thread
                            synchronization. They can also cause memory leaks when used incorrectly.
                            If possible, use `StateFlow` instead.
                """,
                moreInfo = "http://go//sysui-shared-flow",
                category = Category.PERFORMANCE,
                priority = 8,
                severity = Severity.ERROR,
                implementation =
                    Implementation(
                        FlowDetector::class.java,
                        Scope.JAVA_FILE_SCOPE,
                    ),
            )

        fun getTypeOfExtensionMethod(method: PsiMethod): PsiClassType? {
            // If this is an extension method whose return type matches receiver
            val parameterList = method.parameterList
            if (parameterList.parametersCount > 0) {
                val firstParameter = parameterList.getParameter(0)
                if (firstParameter is PsiParameter && firstParameter.name.startsWith("\$this\$")) {
                    return firstParameter.type as? PsiClassType
                }
            }
            return null
        }
    }
}

private const val PACKAGE_FLOW = "kotlinx.coroutines.flow"
private const val FUN_MUTABLE_SHARED_FLOW = "MutableSharedFlow"
private const val FUN_SHARE_IN = "shareIn"
private const val CLASS_FLOW = "$PACKAGE_FLOW.Flow"
+7 −3
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import com.android.tools.lint.detector.api.Issue
import com.android.tools.lint.detector.api.JavaContext
import com.android.tools.lint.detector.api.Location
import com.android.tools.lint.detector.api.Scope
import com.android.tools.lint.detector.api.Severity
import com.android.tools.lint.detector.api.SourceCodeScanner
import java.time.Year
import org.jetbrains.uast.UComment
@@ -117,8 +116,13 @@ class MissingApacheLicenseDetector : Detector(), SourceCodeScanner {
                        attached at the beginning.""",
                category = Category.COMPLIANCE,
                priority = 8,
                // ignored by default and then explicitly overridden in SysUI's soong configuration
                severity = Severity.IGNORE,
                // This check is disabled by default so that it is not accidentally used by internal
                // modules that have different silencing. This check can be enabled in Soong using
                // the following configuration:
                //   lint: {
                //    warning_checks: ["MissingApacheLicenseDetector"],
                //   }
                enabledByDefault = false,
                implementation =
                    Implementation(MissingApacheLicenseDetector::class.java, Scope.JAVA_FILE_SCOPE),
            )
+1 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ class SystemUIIssueRegistry : IssueRegistry() {
                BroadcastSentViaContextDetector.ISSUE,
                CleanArchitectureDependencyViolationDetector.ISSUE,
                DumpableNotRegisteredDetector.ISSUE,
                FlowDetector.SHARED_FLOW_CREATION,
                SlowUserQueryDetector.ISSUE_SLOW_USER_ID_QUERY,
                SlowUserQueryDetector.ISSUE_SLOW_USER_INFO_QUERY,
                NonInjectedMainThreadDetector.ISSUE,
Loading