Modern Apps git

← Modern-Apps.git

lint-rules: add TOAST EXCEPTION per-file opt-out marker

Commit
2daf77dd23ed006fdd6b2b4b05e7c243f25a4e87
Author
vayun-mathur <[email protected]>
When
2026-09-07 03:58 UTC (3 months ago)
Parents
fdaba27

Diff

CONTRIBUTING.md
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 9973114f38..99f3045946 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -299,6 +299,13 @@ AppMessages.show(...)           // from a ViewModel, Worker, or Activity  Use a notification when the message has to outlive the current screen. +Per-file opt-out, for a UI-less component with no scaffold collecting `AppMessages` — a+notification trampoline that finishes immediately, for instance:++```kotlin+// TOAST EXCEPTION: <reason>+```+ ### Never animate directly with `androidx.compose.animation`  The other `fatal` rule, `DirectComposeAnimation`. Motion written at a call site carries no
lint-rules/src/main/java/com/vayunmathur/lint/ToastDetector.kt
diff --git a/lint-rules/src/main/java/com/vayunmathur/lint/ToastDetector.kt b/lint-rules/src/main/java/com/vayunmathur/lint/ToastDetector.ktindex a8ac7cf474..8c9fa5b21d 100644--- a/lint-rules/src/main/java/com/vayunmathur/lint/ToastDetector.kt+++ b/lint-rules/src/main/java/com/vayunmathur/lint/ToastDetector.kt@@ -9,6 +9,7 @@ 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  /**@@ -26,6 +27,10 @@ import org.jetbrains.uast.UCallExpression  *  - `rememberMessenger().show(...)` from a composable  *  - `AppMessages.show(...)` from a ViewModel, worker, or plain Activity  *  - a notification, if the message must survive the user leaving the screen+ *+ * Per-file opt-out: a file carrying `// TOAST EXCEPTION: <reason>` is skipped. This exists for+ * the case the replacements cannot cover - a UI-less component with no scaffold collecting+ * `AppMessages`, such as a notification trampoline that finishes immediately.  */ class ToastDetector : Detector(), SourceCodeScanner { @@ -33,18 +38,26 @@ class ToastDetector : Detector(), SourceCodeScanner {      override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {         if (!context.evaluator.isMemberInClass(method, TOAST_CLASS)) return++        // Documented per-file opt-out, matching the repo's other escape hatches.+        val fileText = (context.uastFile?.sourcePsi as? KtFile)?.text+        if (fileText != null && fileText.contains(EXCEPTION_MARKER)) return+         context.report(             ISSUE,             node,             context.getLocation(node),             "Toast is not allowed. Use `AppMessages.show(...)`, or " +-                "`rememberMessenger().show(...)` in a composable.",+                "`rememberMessenger().show(...)` in a composable. If nothing can collect either " ++                "here, add the `$EXCEPTION_MARKER <reason>` marker comment to the file.",         )     }      companion object {         private const val TOAST_CLASS = "android.widget.Toast" +        private const val EXCEPTION_MARKER = "// TOAST EXCEPTION:"+         val ISSUE: Issue = Issue.create(             id = "ToastUsage",             briefDescription = "Toast is not allowed",@@ -52,6 +65,10 @@ class ToastDetector : Detector(), SourceCodeScanner {                 Toasts render outside the app's Material surface and ignore the app theme,                 cannot carry an action, and are silently suppressed by the system when the                 app is backgrounded on Android 12 and above - so the case people reach for                 a Toast in, reporting from background work, is the case where it may never                 appear.                  Use AppMessages.show(...) from anywhere without a Context,                 rememberMessenger().show(...) inside a composable, or a notification when                 the message has to outlive the current screen.++                If none of those can work - a UI-less component with no scaffold collecting \+                AppMessages, such as a notification trampoline that finishes immediately - add \+                the marker comment `// TOAST EXCEPTION: <reason>` to the file to opt it out.                 """.trimIndent(),             category = Category.USABILITY,             priority = 7,
share/src/main/java/com/vayunmathur/share/platform/receive/ShareSaveActivity.kt
diff --git a/share/src/main/java/com/vayunmathur/share/platform/receive/ShareSaveActivity.kt b/share/src/main/java/com/vayunmathur/share/platform/receive/ShareSaveActivity.ktindex f35a8f2989..fa2286bea9 100644--- a/share/src/main/java/com/vayunmathur/share/platform/receive/ShareSaveActivity.kt+++ b/share/src/main/java/com/vayunmathur/share/platform/receive/ShareSaveActivity.kt@@ -83,6 +83,7 @@ class ShareSaveActivity : ComponentActivity() {         val targets = uris()         val next = targets.getOrNull(index)         if (next == null) {+            // TOAST EXCEPTION: no UI to host a snackbar.             // A Toast, not `AppMessages`: that is collected by the app's scaffold, and this             // trampoline has no UI and may well be the only thing the user has open.             Toast.makeText(