Repository: square/anvil (kotlin)

Issue:
Suppress/OptIn Lint warnings/errors are not ignored in Anvil/src-gen
We have some `@OptIn` Errors/Warnings.  We add to certain classes to make sure Engineers are aware of the fallout from using certain classes, however the Kotlin Compiler will flag/fail on these classes when the anvil/src-gen is compiled.

Not sure the best approach, but I'm guessing either Anvil add's the Suppression/OptIn, or has a compiler flag set to ignore lint warnings? Looks like Dagger/KSP/KAPT might ignore these as they don't warn/fail on these lint checks.

(If theres already a plugin option appologies!)

Pull request description:
It's impossible to suppress these errors in generated code on the file level for the user, so we must opt-out of all these checks. It's not a bad thing, because the user already must opt-into these features on the usage side.

Fixes #488.

Patch:
diff --git a/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/KotlinPoetUtils.kt b/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/KotlinPoetUtils.kt
index a7566ce13..952fce3a5 100644
--- a/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/KotlinPoetUtils.kt
+++ b/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/KotlinPoetUtils.kt
@@ -315,7 +315,23 @@ private fun FileSpec.writeToString(): String {
 }
 
 private fun FileSpec.Builder.suppressWarnings() {
-  addAnnotation(AnnotationSpec.builder(Suppress::class).addMember("\"DEPRECATION\"").build())
+  addAnnotation(
+    AnnotationSpec
+      .builder(Suppress::class)
+      // Suppress deprecation warnings.
+      .addMember("\"DEPRECATION\"")
+      // Suppress errors for experimental features in generated code.
+      .apply {
+        if (KotlinVersion.CURRENT > KotlinVersion(1, 6, 10)) {
+          addMember("\"OPT_IN_USAGE\"")
+          addMember("\"OPT_IN_USAGE_ERROR\"")
+        } else {
+          addMember("\"EXPERIMENTAL_API_USAGE_ERROR\"")
+          addMember("\"EXPERIMENTAL_API_USAGE\"")
+        }
+      }
+      .build()
+  )
 }
 
 @ExperimentalAnvilApi


Test patch:
diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/codegen/CodeGenerationExtensionTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/codegen/CodeGenerationExtensionTest.kt
index cd7e018d9..428517900 100644
--- a/compiler/src/test/java/com/squareup/anvil/compiler/codegen/CodeGenerationExtensionTest.kt
+++ b/compiler/src/test/java/com/squareup/anvil/compiler/codegen/CodeGenerationExtensionTest.kt
@@ -154,4 +154,31 @@ class CodeGenerationExtensionTest {
       assertThat(generateCodeCalled).isFalse()
     }
   }
+
+  @Test fun `errors that require an opt-in annotation are suppressed in generated code`() {
+    compile(
+      """
+      package com.squareup.test
+
+      import com.squareup.anvil.annotations.ContributesBinding
+      import javax.inject.Inject
+
+      @Retention(AnnotationRetention.BINARY)
+      @RequiresOptIn(
+          message = "",
+          level = RequiresOptIn.Level.ERROR
+      )
+      @MustBeDocumented
+      annotation class InternalApi
+
+      interface Type
+
+      @InternalApi
+      @ContributesBinding(Unit::class)
+      class SomeClass @Inject constructor() : Type 
+      """
+    ) {
+      assertThat(exitCode).isEqualTo(OK)
+    }
+  }
 }