package com.vayunmathur.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.PsiMethod import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.uast.UCallExpression import org.jetbrains.uast.UExpression import org.jetbrains.uast.ULambdaExpression import org.jetbrains.uast.ULiteralExpression import org.jetbrains.uast.getParentOfType /** * No dead clicks: a clickable widget whose click handler does nothing. * * A `Button`/`IconButton`/FAB/menu item/list row that renders as tappable * but whose `onClick` lambda is empty (or comment-only) is a broken promise * in the UI: the user taps, nothing happens, and nothing in review or in a * screenshot shows it. This detector flags the empty handler at the call * site in `src/main`. * * Widget set (by simple call name, resolved against Compose/library * containers): `Button`, `TextButton`, `OutlinedButton`, * `FilledTonalButton`, `ElevatedButton`, the FAB family, `IconButton`, * `IconToggleButton`, `DropdownMenuItem`, `MenuItem`, `SegmentedButton`, * and the TV row family (`TvButton`, `TvListRow`, `ListItem`). A widget * call's `Modifier.clickable`/`combinedClickable` is covered the same way * when it sits in that call's modifier (the list-row shape), and a bare * `clickable`/`combinedClickable` call is flagged wherever it appears. * * Precision choices (deliberate): * - `enabled = false` in the same call opts out: a disabled widget with an * empty handler is honest — it does not promise a tap. (Busy-state * buttons and display rows use this.) * - Display-only label chips are excluded by widget type: `AssistChip`, * `FilterChip`, chips generally are simply not in the widget set — the * repo renders read-only labels with them and they carry no click * promise this rule polices. Fix those by demoting the chip, not here. * - A `clickable`/`combinedClickable` with a non-empty `onLongClick` is * not dead: long-press-only rows (copy-on-long-press) work. * - Only a literal empty/comment-only lambda is flagged. A handler passed * as a reference (`onClick = onOpen`, `onClick = actions::save`) or a * parameter default is never guessed at. * * Fix, in order of preference: wire the handler to the real action; * disable the widget honestly (`enabled = false`); or demote it to a * non-button container (a `Box`/`Icon` with the same modifier). The one * legitimate shape left — a drag handle grip that must look like a button * while the reorder modifier owns the gesture — opts out per file with: * `// DEAD CLICK EXCEPTION: `. */ class NoDeadClickDetector : Detector(), SourceCodeScanner { override fun getApplicableMethodNames(): List = APPLICABLE.toList() override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) { val name = method.name val file = context.file if (!file.path.replace('\\', '/').contains("/src/main/")) return val packageName = context.uastFile?.packageName ?: return if (!packageName.startsWith(APP_PACKAGE_PREFIX)) return val ktFile = context.uastFile?.sourcePsi as? KtFile if (ktFile != null && CommentOnlyMatcher.containsMarkerInComments(ktFile, EXCEPTION_MARKER)) return if (name in CLICKABLE_CALLS) { checkClickable(context, node, method, enclosingWidgetName(node) ?: name) return } if (name !in WIDGET_CALLS) return if (!isWidgetContainer(method)) return if (hasDisabledFalse(node, method)) return // A clickable in this widget's own modifier (the list-row shape). var flagged = false for (arg in node.valueArguments) { val click = findNestedClickable(arg) ?: continue if (isDeadClickable(click, null)) { report(context, click, name) flagged = true } } if (flagged) return val onClick = argumentFor(node, method, "onClick") ?: return if (isEmptyLambda(onClick)) { report(context, node, name) } } private fun checkClickable(context: JavaContext, node: UCallExpression, method: PsiMethod, widgetName: String) { if (!isDeadClickable(node, method)) return // Widget calls check their own modifier's clickable (above); do not // double-report the same lambda from both visits. val parentCall = node.getParentOfType(strict = true) if (parentCall != null && parentCall.methodName in WIDGET_CALLS) return report(context, node, widgetName) } private fun report(context: JavaContext, node: UCallExpression, widgetName: String) { context.report( ISSUE, node, context.getLocation(node), "Dead click: `$widgetName` renders as tappable but its click handler is " + "empty, so tapping it does nothing. Wire it to the real action, disable " + "it honestly (`enabled = false`), or demote it to a non-button " + "container. A drag-handle grip whose reorder modifier owns the gesture " + "opts out per file with `$EXCEPTION_MARKER `.", ) } /** True for an empty/comment-only `onClick` with no working `onLongClick` and not disabled. */ private fun isDeadClickable(node: UCallExpression, method: PsiMethod?): Boolean { if (method != null && hasDisabledFalse(node, method)) return false val onLongClick = method?.let { argumentFor(node, it, "onLongClick") } ?: argumentByName(node, "onLongClick") if (onLongClick != null && !isEmptyLambda(onLongClick) && !isNullLiteral(onLongClick)) return false val onClick = (method?.let { argumentFor(node, it, "onClick") } ?: argumentByName(node, "onClick")) // Trailing lambda: Modifier.clickable { … } ?: node.valueArguments.lastOrNull()?.takeIf { it is ULambdaExpression } ?: return false return isEmptyLambda(onClick) } /** The nearest enclosing widget call's name, for the message (list-row modifier shape). */ private fun enclosingWidgetName(node: UCallExpression): String? { var parent = node.getParentOfType(strict = true) while (parent != null) { val n = parent.methodName if (n in WIDGET_CALLS) return n parent = parent.getParentOfType(strict = true) } return null } private fun findNestedClickable(expr: UExpression): UCallExpression? { if (expr is UCallExpression && expr.methodName in CLICKABLE_CALLS) return expr // Modifier chains are nested calls (`.clickable{}.then(…)` etc.): // walk the UAST subtree for the first clickable call. var found: UCallExpression? = null expr.accept(object : org.jetbrains.uast.visitor.AbstractUastVisitor() { override fun visitCallExpression(node: UCallExpression): Boolean { if (found == null && node !== expr && node.methodName in CLICKABLE_CALLS) { found = node } return super.visitCallExpression(node) } }) return found } private fun hasDisabledFalse(node: UCallExpression, method: PsiMethod): Boolean { val enabled = argumentFor(node, method, "enabled") ?: return false return enabled is ULiteralExpression && enabled.value == false } private fun isNullLiteral(expr: UExpression): Boolean = expr is ULiteralExpression && expr.isNull /** * Empty or comment-only lambda. UAST drops comments from the lambda * body, so a comment-only lambda and `{}` both have no expressions; * a body holding only another lambda's braces still counts as work * only if it has expressions — checked recursively by UAST shape. */ private fun isEmptyLambda(expr: UExpression): Boolean { val lambda = expr as? ULambdaExpression ?: return false val body = lambda.body as? org.jetbrains.uast.UBlockExpression ?: return false return body.expressions.isEmpty() } /** Argument by parameter name via the resolved method's parameter list. */ private fun argumentFor(node: UCallExpression, method: PsiMethod, paramName: String): UExpression? { val index = method.parameterList.parameters.indexOfFirst { it.name == paramName } if (index < 0) return argumentByName(node, paramName) return node.getArgumentForParameter(index) } /** * Argument by source-level named-argument shape: lint's UAST keeps * named arguments in [org.jetbrains.uast.UNamedExpression] wrappers in * some shapes and plain values in others, so match on the argument's * own name when present and fall back to the last lambda (trailing). */ private fun argumentByName(node: UCallExpression, paramName: String): UExpression? { for (arg in node.valueArguments) { val named = arg as? org.jetbrains.uast.UNamedExpression if (named?.name == paramName) return named.expression } return null } private fun isWidgetContainer(method: PsiMethod): Boolean { val containing = method.containingClass?.qualifiedName ?: return true return containing.contains("compose") || containing.startsWith("com.vayunmathur.library") || containing.contains("androidx.tv") || containing.contains("androidx.wear") } companion object { private const val APP_PACKAGE_PREFIX = "com.vayunmathur." const val EXCEPTION_MARKER = "// DEAD CLICK EXCEPTION:" private val WIDGET_CALLS = setOf( "Button", "TextButton", "OutlinedButton", "FilledTonalButton", "ElevatedButton", "FloatingActionButton", "SmallFloatingActionButton", "LargeFloatingActionButton", "ExtendedFloatingActionButton", "SmallExtendedFloatingActionButton", "IconButton", "IconToggleButton", "DropdownMenuItem", "MenuItem", "SegmentedButton", "TvButton", "TvListRow", "ListItem", ) private val CLICKABLE_CALLS = setOf("clickable", "combinedClickable") private val APPLICABLE = WIDGET_CALLS + CLICKABLE_CALLS val ISSUE: Issue = Issue.create( id = "NoDeadClick", briefDescription = "Clickable widget with an empty click handler", explanation = """ A Button, IconButton, FAB, menu item, segmented button, TV row, \ ListItem, or Modifier.clickable/combinedClickable whose onClick lambda \ is empty (or comment-only) renders as tappable and does nothing when \ tapped. Wire the handler to the real action, disable the widget \ honestly with `enabled = false`, or demote it to a non-button \ container. Display-only label chips are excluded by widget type, \ `enabled = false` widgets are honest, and long-press-only rows with \ a working onLongClick are not dead. A drag-handle grip whose reorder \ modifier owns the gesture opts out per file with \ `// DEAD CLICK EXCEPTION: `. """.trimIndent(), category = Category.CORRECTNESS, priority = 6, severity = Severity.ERROR, implementation = Implementation( NoDeadClickDetector::class.java, Scope.JAVA_FILE_SCOPE, ), ) } }