1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 @file:Suppress("unused", "ObjectLiteralToLambda", "UNUSED_VARIABLE")
17 
18 package sample.optin
19 
20 /**
21  * Regression test for b/192562469 where the lint check does not handle annotation usage in lambdas.
22  */
23 class RegressionTestKotlin192562469 {
24     @ExperimentalKotlinAnnotation
25     internal fun interface ExperimentalInterface {
experimentalMethodnull26         fun experimentalMethod()
27     }
28 
29     /** Unsafe usage due to implementation of an experimental interface. */
30     internal class ConcreteExperimentalInterface : ExperimentalInterface { // unsafe
31         override fun experimentalMethod() {} // unsafe override
32     }
33 
34     /** Safe usage due to opt-in. */
35     @OptIn(ExperimentalKotlinAnnotation::class)
36     internal class ConcreteExperimentalInterfaceOptIn : ExperimentalInterface {
experimentalMethodnull37         override fun experimentalMethod() {}
38     }
39 
40     /** Safe usage due to propagation. */
41     @ExperimentalKotlinAnnotation
42     internal class ConcreteExperimentalInterfacePropagate : ExperimentalInterface {
experimentalMethodnull43         override fun experimentalMethod() {}
44     }
45 
46     /** Unsafe implementations of an experimental interface. */
regressionTestOverridesnull47     fun regressionTestOverrides() {
48         val anonymous: ExperimentalInterface =
49             object : ExperimentalInterface { // unsafe
50                 override fun experimentalMethod() {} // unsafe override
51             }
52         val lambda = ExperimentalInterface {} // unsafe
53     }
54 
55     /** Safe implementations of an experimental interface due to opt-in. */
56     @OptIn(ExperimentalKotlinAnnotation::class)
regressionTestOverridesOptInnull57     fun regressionTestOverridesOptIn() {
58         val anonymous: ExperimentalInterface =
59             object : ExperimentalInterface {
60                 override fun experimentalMethod() {} // safe
61             }
62         val lambda = ExperimentalInterface {} // safe
63     }
64 
65     /** Safe implementations of an experimental interface due to propagation. */
66     @ExperimentalKotlinAnnotation
regressionTestOverridesPropagatenull67     fun regressionTestOverridesPropagate() {
68         val anonymous: ExperimentalInterface =
69             object : ExperimentalInterface {
70                 override fun experimentalMethod() {} // safe
71             }
72         val lambda = ExperimentalInterface {} // safe
73     }
74 }
75