/
UnusedImportsSpec.kt
669 lines (567 loc) · 17.9 KB
/
UnusedImportsSpec.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
import io.gitlab.arturbosch.detekt.test.lintWithContext
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.Test
@KotlinCoreEnvironmentTest
class UnusedImportsSpec(val env: KotlinCoreEnvironment) {
val subject = UnusedImports(Config.empty)
@Test
fun `does not report infix operators`() {
val main = """
import tasks.success
fun task(f: () -> Unit) = 1
fun main() {
task {
} success {
}
}
"""
val additional = """
package tasks
infix fun Int.success(f: () -> Unit) {}
"""
assertThat(subject.lintWithContext(env, main, additional)).isEmpty()
}
@Test
fun `does not report imports in documentation`() {
val main = """
import tasks.success
import tasks.failure
import tasks.undefined
fun task(f: () -> Unit) = 1
/**
* Reference to [failure]
*/
class Test {
/** Reference to [undefined]*/
fun main() {
task {
} success {
}
}
}
"""
val additional = """
package tasks
infix fun Int.success(f: () -> Unit) {}
infix fun Int.failure(f: () -> Unit) {}
infix fun Int.undefined(f: () -> Unit) {}
"""
assertThat(subject.lintWithContext(env, main, additional)).isEmpty()
}
@Test
fun `should ignore import for link`() {
val main = """
import tasks.success
import tasks.failure
import tasks.undefined
fun task(f: () -> Unit) = 1
/**
* Reference [undefined][failure]
*/
fun main() {
task {
} success {
}
}
"""
val additional = """
package tasks
infix fun Int.success(f: () -> Unit) {}
infix fun Int.failure(f: () -> Unit) {}
infix fun Int.undefined(f: () -> Unit) {}
"""
val lint = subject.lintWithContext(env, main, additional)
with(lint) {
assertThat(this).hasSize(1)
assertThat(this[0].entity.signature).endsWith("import tasks.undefined")
}
}
@Test
fun `reports imports from the current package`() {
val main = """
package test
import test.SomeClass
val a: SomeClass? = null
"""
val additional = """
package test
class SomeClass
"""
val lint = subject.lintWithContext(env, main, additional)
with(lint) {
assertThat(this).hasSize(1)
assertThat(this[0].entity.signature).endsWith("import test.SomeClass")
}
}
@Test
fun `does not report KDoc references with method calls`() {
val main = """
package com.example
import android.text.TextWatcher
class Test {
/**
* [TextWatcher.beforeTextChanged]
*/
fun test() {
TODO()
}
}
"""
val additional = """
package android.text
class TextWatcher {
fun beforeTextChanged() {}
}
"""
assertThat(subject.lintWithContext(env, main, additional)).isEmpty()
}
@Test
fun `reports imports with different cases`() {
val main = """
import p.a
import p.B6 // positive
import p.B as B12 // positive
import p2.B as B2
import p.C
import escaped.`when`
import escaped.`foo` // positive
import p.D
/** reference to [D] */
fun main() {
println(a())
C.call()
fn(B2.NAME)
`when`()
}
fun fn(s: String) {}
"""
val p = """
package p
fun a() {}
class B6
class B
object C {
fun call() {}
}
class D
"""
val p2 = """
package p2
object B {
const val NAME = ""
}
"""
val escaped = """
package escaped
fun `when`() {}
fun `foo`() {}
"""
val lint = subject.lintWithContext(env, main, p, p2, escaped)
with(lint) {
assertThat(this).hasSize(3)
assertThat(this[0].entity.signature).contains("import p.B6")
assertThat(this[1].entity.signature).contains("import p.B as B12")
assertThat(this[2].entity.signature).contains("import escaped.`foo`")
}
}
@Test
fun `does not report imports in same package when inner`() {
val main = """
package test
import test.Outer.Inner
open class Something<T>
class Foo : Something<Inner>()
"""
val additional = """
package test
class Outer {
class Inner
}
"""
val lint = subject.lintWithContext(env, main, additional)
with(lint) {
assertThat(this).isEmpty()
}
}
@Test
fun `does not report KDoc @see annotation linking to class`() {
val main = """
import tasks.success
/**
* Do something.
* @see success
*/
fun doSomething()
"""
val additional = """
package tasks
fun success() {}
"""
assertThat(subject.lintWithContext(env, main, additional)).isEmpty()
}
@Test
fun `does not report KDoc @see annotation linking to class with description`() {
val main = """
import tasks.success
/**
* Do something.
* @see success something
*/
fun doSomething() {}
"""
val additional = """
package tasks
fun success() {}
"""
assertThat(subject.lintWithContext(env, main, additional)).isEmpty()
}
@Test
fun `reports KDoc @see annotation that does not link to class`() {
val main = """
import tasks.success
/**
* Do something.
* @see something
*/
fun doSomething() {}
"""
val additional = """
package tasks
fun success() {}
"""
assertThat(subject.lintWithContext(env, main, additional)).hasSize(1)
}
@Test
fun `reports KDoc @see annotation that links after description`() {
val main = """
import tasks.success
/**
* Do something.
* @see something success
*/
fun doSomething() {}
"""
val additional = """
package tasks
fun success() {}
"""
assertThat(subject.lintWithContext(env, main, additional)).hasSize(1)
}
@Test
fun `does not report imports in KDoc`() {
val main = """
import tasks.success // here
import tasks.undefined // and here
/**
* Do something.
* @throws success when ...
* @exception success when ...
* @see undefined
* @sample success when ...
*/
fun doSomething() {}
"""
val additional = """
package tasks
fun success() {}
fun undefined() {}
"""
assertThat(subject.lintWithContext(env, main, additional)).isEmpty()
}
@Test
fun `should not report import alias as unused when the alias is used`() {
val main = """
import test.forEach as foreach
fun foo() = listOf().iterator().foreach {}
"""
val additional = """
package test
fun Iterator<Int>.forEach(f: () -> Unit) {}
"""
assertThat(subject.lintWithContext(env, main, additional)).isEmpty()
}
@Test
fun `should not report used alias even when import is from same package`() {
val main = """
package com.example
import com.example.foo as myFoo // from same package but with alias, check alias usage
import com.example.other.foo as otherFoo // not from package with used alias
fun f(): Boolean {
return myFoo() == otherFoo()
}
"""
val additional1 = """
package com.example
fun foo() = 1
"""
val additional2 = """
package com.example.other
fun foo() = 1
"""
assertThat(subject.lintWithContext(env, main, additional1, additional2)).isEmpty()
}
@Test
fun `should not report import of provideDelegate operator overload - #1608`() {
val main = """
import org.gradle.kotlin.dsl.Foo
import org.gradle.kotlin.dsl.provideDelegate // this line specifically should not be reported
class DumpVersionProperties {
private val dumpVersionProperties by Foo()
}
"""
val additional = """
package org.gradle.kotlin.dsl
import kotlin.reflect.KProperty
class Foo
operator fun <T> Foo.provideDelegate(
thisRef: T,
prop: KProperty<*>
) = lazy { "" }
"""
assertThat(subject.lintWithContext(env, main, additional)).isEmpty()
}
@Test
fun `should not report import of componentN operator`() {
val main = """
import com.example.MyClass.component1
import com.example.MyClass.component2
import com.example.MyClass.component543
fun test() {
val (a, b) = MyClass(1, 2)
}
"""
val additional = """
package com.example
data class MyClass(val a: Int, val b: Int)
"""
assertThat(subject.lintWithContext(env, main, additional)).isEmpty()
}
@Test
fun `should report import of identifiers with component in the name`() {
val main = """
import com.example.TestComponent
import com.example.component1.Unused
import com.example.components
import com.example.component1AndSomethingElse
fun test() {
println("Testing")
}
"""
val additional1 = """
package com.example
class TestComponent
fun components() {}
fun component1AndSomethingElse() {}
"""
val additional2 = """
package com.example.component1
class Unused
"""
val lint = subject.lintWithContext(env, main, additional1, additional2)
with(lint) {
assertThat(this).hasSize(4)
assertThat(this[0].entity.signature).endsWith("import com.example.TestComponent")
assertThat(this[1].entity.signature).endsWith("import com.example.component1.Unused")
assertThat(this[2].entity.signature).endsWith("import com.example.components")
assertThat(this[3].entity.signature).endsWith("import com.example.component1AndSomethingElse")
}
}
@Test
fun `reports when same name identifiers are imported and used`() {
val mainFile = """
import foo.test
import bar.test
fun main() {
test(1)
}
"""
val additionalFile1 = """
package foo
fun test(i: Int) {}
"""
val additionalFile2 = """
package bar
fun test(s: String) {}
"""
val findings = subject.lintWithContext(env, mainFile, additionalFile1, additionalFile2)
assertThat(findings).hasSize(1)
assertThat(findings[0].entity.signature).endsWith("import bar.test")
}
@Test
fun `does not report when used as a type`() {
val code = """
import java.util.HashMap
fun doesNothing(thing: HashMap<String, String>) {
}
"""
val findings = subject.lintWithContext(env, code)
assertThat(findings).isEmpty()
}
@Test
fun `does not report when used in a class literal expression`() {
val code = """
import java.util.HashMap
import kotlin.reflect.KClass
annotation class Ann(val value: KClass<*>)
@Ann(HashMap::class)
fun foo() {}
"""
val findings = subject.lintWithContext(env, code)
assertThat(findings).isEmpty()
}
@Test
fun `does not report when used as a constructor call`() {
val mainFile = """
import x.y.z.Foo
val foo = Foo()
"""
val additionalFile = """
package x.y.z
class Foo
"""
val findings = subject.lintWithContext(env, mainFile, additionalFile)
assertThat(findings).isEmpty()
}
@Test
fun `does not report when used as a annotation`() {
val mainFile = """
import x.y.z.Ann
@Ann
fun foo() {}
"""
val additionalFile = """
package x.y.z
annotation class Ann
"""
val findings = subject.lintWithContext(env, mainFile, additionalFile)
assertThat(findings).isEmpty()
}
@Test
fun `does not report companion object`() {
val mainFile = """
import x.y.z.Foo
val x = Foo
"""
val additionalFile = """
package x.y.z
class Foo {
companion object
}
"""
val findings = subject.lintWithContext(env, mainFile, additionalFile)
assertThat(findings).isEmpty()
}
@Test
fun `does not report companion object that calls function`() {
val mainFile = """
import x.y.z.Foo
val x = Foo.create()
"""
val additionalFile = """
package x.y.z
class Foo {
companion object {
fun create(): Foo = Foo()
}
}
"""
val findings = subject.lintWithContext(env, mainFile, additionalFile)
assertThat(findings).isEmpty()
}
@Test
fun `does not report companion object that references variable`() {
val mainFile = """
import x.y.z.Foo
val x = Foo.BAR
"""
val additionalFile = """
package x.y.z
class Foo {
companion object {
const val BAR = 1
}
}
"""
val findings = subject.lintWithContext(env, mainFile, additionalFile)
assertThat(findings).isEmpty()
}
@Test
fun `does not report static import`() {
val mainFile = """
import x.y.z.FetchType
val x = FetchType.LAZY
"""
val additionalFile = """
package x.y.z
enum class FetchType {
LAZY
}
"""
assertThat(subject.lintWithContext(env, mainFile, additionalFile)).isEmpty()
}
@Test
fun `does not report annotations used as attributes - #3246`() {
val mainFile = """
import x.y.z.AnnotationA
import x.y.z.AnnotationB
class SomeClass {
@AnnotationB(attribute = AnnotationA())
val someProp: Int = 42
}
"""
val additionalFile = """
package x.y.z
annotation class AnnotationA
annotation class AnnotationB(val attribute: AnnotationA)
"""
assertThat(subject.lintWithContext(env, mainFile, additionalFile)).isEmpty()
}
@Test
fun `does not report unused import for import used in kdoc - #4815`() {
val mainFile = """
import x.y.z.SomeClass
class MyView
/**
* Style for [MyView]
* Blablabla
*
* @property someVal Someval for [SomeClass]
*/
data class StyleClass(val someVal: String)
"""
val additionalFile = """
package x.y.z
class SomeClass
"""
assertThat(subject.lintWithContext(env, mainFile, additionalFile)).isEmpty()
}
@Test
fun `does not report imports which detekt cannot resolve but have string matches`() {
val mainFile = """
import x.y.z.foo
import x.y.z.Bar
fun test() {
foo()
foo("", 123)
foo
Bar().baz()
}
"""
assertThat(subject.lintWithContext(env, mainFile)).isEmpty()
}
@Test
fun `reports imports which detekt cannot resolve and do not have string matches`() {
val mainFile = """
import x.y.z.foo
import x.y.z.Bar
fun test() {
2 + 3
}
"""
assertThat(subject.lintWithContext(env, mainFile)).hasSize(2)
}
}