@@ -2,9 +2,13 @@ package io.gitlab.arturbosch.detekt.rules.bugs
2
2
3
3
import io.gitlab.arturbosch.detekt.api.Config
4
4
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
5
+ import io.gitlab.arturbosch.detekt.test.TestConfig
5
6
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
7
+ import io.gitlab.arturbosch.detekt.test.lintWithContext
8
+ import io.gitlab.arturbosch.detekt.test.location
6
9
import org.assertj.core.api.Assertions.assertThat
7
10
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
11
+ import org.junit.jupiter.api.Nested
8
12
import org.junit.jupiter.api.Test
9
13
10
14
@KotlinCoreEnvironmentTest
@@ -48,4 +52,136 @@ class DeprecationSpec(private val env: KotlinCoreEnvironment) {
48
52
""" .trimIndent()
49
53
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
50
54
}
55
+
56
+ @Test
57
+ fun `report when property delegate is deprecated` () {
58
+ val stateFile = """
59
+ package state
60
+
61
+ import kotlin.reflect.KProperty
62
+
63
+ interface State {
64
+ val value: Double
65
+ }
66
+
67
+ @Deprecated("Some reason")
68
+ operator fun State.getValue(thisObj: Any?, property: KProperty<*>): Double = value
69
+ """ .trimIndent()
70
+ val code = """
71
+ import state.State
72
+ import state.getValue
73
+ fun foo(state: State) {
74
+ val d by state
75
+ }
76
+ """ .trimIndent()
77
+ assertThat(subject.lintWithContext(env, code, stateFile))
78
+ .hasSize(1 )
79
+ .first()
80
+ .extracting {
81
+ it.message
82
+ }
83
+ .isEqualTo(""" state is deprecated with message "Some reason"""" )
84
+ }
85
+
86
+ @Test
87
+ fun `does not report import location` () {
88
+ val deprecatedFile = """
89
+ package com.example.detekt.featureflag
90
+
91
+ @Deprecated("Moved to other service")
92
+ enum class LegacyFeatureFlags {
93
+ FEATURE_A, FEATURE_B,
94
+ }
95
+ """ .trimIndent()
96
+ val code = """
97
+ import com.example.detekt.featureflag.LegacyFeatureFlags
98
+ class FeatureFlagManager {
99
+ @Suppress("DEPRECATION")
100
+ fun getFeatureFlagValue(featureFlag: LegacyFeatureFlags): Boolean {
101
+ return true
102
+ }
103
+ }
104
+
105
+ class Manager(private val featureFlagManager: FeatureFlagManager) {
106
+ fun doSomething() {
107
+ @Suppress("DEPRECATION")
108
+ val isFeatureAEnabled =
109
+ featureFlagManager.getFeatureFlagValue(LegacyFeatureFlags.FEATURE_A)
110
+ }
111
+ }
112
+ """ .trimIndent()
113
+ assertThat(subject.lintWithContext(env, code, deprecatedFile))
114
+ .hasSize(1 )
115
+ .first()
116
+ .extracting { it.location.source.line }
117
+ .isEqualTo(1 )
118
+ }
119
+
120
+ @Nested
121
+ inner class `With ignore import true ` {
122
+ private val ignoredImportSubject =
123
+ Deprecation (TestConfig (IGNORED_IMPORT to true ))
124
+
125
+ @Test
126
+ fun `does not report import location - #7402` () {
127
+ val deprecatedFile = """
128
+ package com.example.detekt.featureflag
129
+
130
+ @Deprecated("Moved to other service")
131
+ enum class LegacyFeatureFlags {
132
+ FEATURE_A, FEATURE_B,
133
+ }
134
+ """ .trimIndent()
135
+ val code = """
136
+ import com.example.detekt.featureflag.LegacyFeatureFlags
137
+ class FeatureFlagManager {
138
+ @Suppress("DEPRECATION")
139
+ fun getFeatureFlagValue(featureFlag: LegacyFeatureFlags): Boolean {
140
+ return true
141
+ }
142
+ }
143
+
144
+ class Manager(private val featureFlagManager: FeatureFlagManager) {
145
+ fun doSomething() {
146
+ @Suppress("DEPRECATION")
147
+ val isFeatureAEnabled =
148
+ featureFlagManager.getFeatureFlagValue(LegacyFeatureFlags.FEATURE_A)
149
+ }
150
+ }
151
+ """ .trimIndent()
152
+ assertThat(ignoredImportSubject.lintWithContext(env, code, deprecatedFile))
153
+ .isEmpty()
154
+ }
155
+
156
+ @Test
157
+ fun `report when property delegate is deprecated` () {
158
+ val stateFile = """
159
+ package state
160
+ import kotlin.reflect.KProperty
161
+ interface State {
162
+ val value: Double
163
+ }
164
+ @Deprecated("Some reason")
165
+ operator fun State.getValue(thisObj: Any?, property: KProperty<*>): Double = value
166
+ """ .trimIndent()
167
+ val code = """
168
+ import state.State
169
+ import state.getValue
170
+ fun foo(state: State) {
171
+ val d by state
172
+ }
173
+ """ .trimIndent()
174
+ assertThat(ignoredImportSubject.lintWithContext(env, code, stateFile))
175
+ .hasSize(1 )
176
+ .first()
177
+ .extracting {
178
+ it.message
179
+ }
180
+ .isEqualTo(""" state is deprecated with message "Some reason"""" )
181
+ }
182
+ }
183
+
184
+ companion object {
185
+ private const val IGNORED_IMPORT = " ignoreImport"
186
+ }
51
187
}
0 commit comments