Fix heuristic for detecting permission name in documentation

Previously, the heuristic would look for the permission's unqualified
name in the documentation. That meant if the permission was `DUMP` it
would think that `DUMP_FILE` or `VERBOSE_DUMP` was referencing the
permission when it clearly is not. This change fixes that by only
looking for the permission as a whole word.

Bug: 367787970
Test: ./gradlew
Change-Id: I6fbcc7e0c7175dfc5c56ecfe24167e273f797876
diff --git a/metalava/src/main/java/com/android/tools/metalava/AndroidApiChecks.kt b/metalava/src/main/java/com/android/tools/metalava/AndroidApiChecks.kt
index 552fc8e..70925b4 100644
--- a/metalava/src/main/java/com/android/tools/metalava/AndroidApiChecks.kt
+++ b/metalava/src/main/java/com/android/tools/metalava/AndroidApiChecks.kt
@@ -212,9 +212,10 @@
                 }
             }
             for (item in permissions) {
-                var perm = item
-                if (perm.indexOf('.') >= 0) perm = perm.substring(perm.lastIndexOf('.') + 1)
-                val mentioned = text.contains(perm)
+                val perm = item.substringAfterLast('.')
+                // Search for the permission name as a whole word.
+                val regex = Regex("""\b\Q$perm\E\b""")
+                val mentioned = text.contains(regex)
                 if (mentioned && !conditional) {
                     reporter.report(
                         Issues.REQUIRES_PERMISSION,
diff --git a/metalava/src/test/java/com/android/tools/metalava/AndroidApiChecksTest.kt b/metalava/src/test/java/com/android/tools/metalava/AndroidApiChecksTest.kt
index c7ce30f..ff1d791 100644
--- a/metalava/src/test/java/com/android/tools/metalava/AndroidApiChecksTest.kt
+++ b/metalava/src/test/java/com/android/tools/metalava/AndroidApiChecksTest.kt
@@ -194,13 +194,6 @@
                 ),
             extraArguments =
                 arrayOf(ARG_WARNING, Issues.CONDITIONAL_REQUIRES_PERMISSION_NOT_EXPLAINED.name),
-            expectedFail = DefaultLintErrorMessage,
-            // TODO(b/367787970): Stop reporting issues when permission is just a subset of a word.
-            expectedIssues =
-                """
-                    src/android/pkg/PermissionTest.java:12: error: Method 'test0' documentation duplicates auto-generated documentation by @RequiresPermission. If the permissions are only required under certain circumstances use conditional=true to suppress the auto-documentation [RequiresPermission]
-                    src/android/pkg/PermissionTest.java:20: error: Method 'test0' documentation duplicates auto-generated documentation by @RequiresPermission. If the permissions are only required under certain circumstances use conditional=true to suppress the auto-documentation [RequiresPermission]
-                """,
         )
     }
 
diff --git a/metalava/src/test/resources/source-model-provider-baseline.txt b/metalava/src/test/resources/source-model-provider-baseline.txt
index 8caf026..a6dafa8 100644
--- a/metalava/src/test/resources/source-model-provider-baseline.txt
+++ b/metalava/src/test/resources/source-model-provider-baseline.txt
@@ -1,5 +1,4 @@
 com.android.tools.metalava.AndroidApiChecksTest
-  Document Permissions ignore when permission is subset of a word[turbine]
   Document Permissions[turbine]
 
 com.android.tools.metalava.AnnotationsMergerTest