Refactoring: make "replace get or set" an inspection

This commit is contained in:
Mikhail Glukhikh
2017-12-19 14:45:15 +03:00
parent 756cb32eaf
commit 91bcfb97c6
50 changed files with 375 additions and 342 deletions
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.conventionNameCalls.ReplaceGetOrSetInspection
@@ -0,0 +1,7 @@
fun test() {
class Test{
operator fun get(a: Int, vararg b: Int, c: Int = 0) : Int = 0
}
val test = Test()
test.g<caret>et(1, 3, 4, 5)
}
@@ -0,0 +1,7 @@
fun test() {
class Test{
operator fun get(a: Int, vararg b: Int, c: Int = 0) : Int = 0
}
val test = Test()
test<caret>[1, 3, 4, 5]
}
@@ -0,0 +1,20 @@
fun test() {
class Test{
operator fun get(a: Int, b: Int, fn: (i: Int) -> Int) : Int = 0
}
val test = Test()
test.g<caret>et(1, 2) { i ->
i
}
}
fun withSuppression() {
class Test{
operator fun get(a: Int, b: Int, fn: (i: Int) -> Int) : Int = 0
}
val test = Test()
@Suppress("ReplaceGetOrSet")
test.get(1, 2) { i -> i }
}
@@ -0,0 +1,20 @@
fun test() {
class Test{
operator fun get(a: Int, b: Int, fn: (i: Int) -> Int) : Int = 0
}
val test = Test()
test<caret>[1, 2, { i ->
i
}]
}
fun withSuppression() {
class Test{
operator fun get(a: Int, b: Int, fn: (i: Int) -> Int) : Int = 0
}
val test = Test()
@Suppress("ReplaceGetOrSet")
test.get(1, 2) { i -> i }
}
@@ -0,0 +1,10 @@
// PROBLEM: none
// ERROR: An argument is already passed for this parameter
// ERROR: No value passed for parameter 'b'
fun test() {
class Test{
operator fun get(a: Int, b: Int) : Int = 0
}
val test = Test()
test.g<caret>et(a=0, a=1)
}
@@ -0,0 +1,8 @@
fun test() {
class Test()
operator fun Test.get(i: Int) : Int = 0
val test = Test()
test.g<caret>et(0)
}
@@ -0,0 +1,8 @@
fun test() {
class Test()
operator fun Test.get(i: Int) : Int = 0
val test = Test()
test<caret>[0]
}
@@ -0,0 +1,9 @@
fun test() {
class Test{
operator fun get(fn: (i: Int) -> Int) : Int = 0
}
val test = Test()
test.g<caret>et() { i ->
i
}
}
@@ -0,0 +1,9 @@
fun test() {
class Test{
operator fun get(fn: (i: Int) -> Int) : Int = 0
}
val test = Test()
test<caret>[{ i ->
i
}]
}
@@ -0,0 +1,82 @@
<problems>
<problem>
<file>singleArgument.kt</file>
<line>8</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="singleArgument.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Explicit 'get' or 'set' call</problem_class>
<description>Call replaceable with indexing operator</description>
</problem>
<problem>
<file>multiArgument.kt</file>
<line>6</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="multiArgument.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Explicit 'get' or 'set' call</problem_class>
<description>Call replaceable with indexing operator</description>
</problem>
<problem>
<file>functionalArgument.kt</file>
<line>6</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="functionalArgument.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Explicit 'get' or 'set' call</problem_class>
<description>Call replaceable with indexing operator</description>
</problem>
<problem>
<file>extensionFunction.kt</file>
<line>7</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="extensionFunction.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Explicit 'get' or 'set' call</problem_class>
<description>Call replaceable with indexing operator</description>
</problem>
<problem>
<file>argumentAndFunction.kt</file>
<line>6</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="argumentAndFunction.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Explicit 'get' or 'set' call</problem_class>
<description>Call replaceable with indexing operator</description>
</problem>
<problem>
<file>acceptableVararg.kt</file>
<line>6</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="acceptableVararg.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Explicit 'get' or 'set' call</problem_class>
<description>Call replaceable with indexing operator</description>
</problem>
<problem>
<file>qualifier.kt</file>
<line>8</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="qualifier.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Explicit 'get' or 'set' call</problem_class>
<description>Call replaceable with indexing operator</description>
</problem>
<problem>
<file>set.kt</file>
<line>8</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="set.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Explicit 'get' or 'set' call</problem_class>
<description>Call replaceable with indexing operator</description>
</problem>
<problem>
<file>set2.kt</file>
<line>9</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="set2.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Explicit 'get' or 'set' call</problem_class>
<description>Call replaceable with indexing operator</description>
</problem>
</problems>
@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.conventionNameCalls.ReplaceGetOrSetInspection
@@ -0,0 +1,9 @@
// PROBLEM: none
// ERROR: Cannot find a parameter with this name: c
fun test() {
class Test{
operator fun get(a: Int=1, b: Int=2) : Int = 0
}
val test = Test()
test.g<caret>et(c=3)
}
@@ -0,0 +1,8 @@
// PROBLEM: none
fun test() {
class Test{
operator fun get(a: Int=1, b: Int=2) : Int = 0
}
val test = Test()
test.g<caret>et(b=3)
}
@@ -0,0 +1,7 @@
fun test() {
class Test{
operator fun get(a: Int, b: Int) : Int = 0
}
val test = Test()
test.g<caret>et(1, 2)
}
@@ -0,0 +1,7 @@
fun test() {
class Test{
operator fun get(a: Int, b: Int) : Int = 0
}
val test = Test()
test<caret>[1, 2]
}
@@ -0,0 +1,9 @@
// PROBLEM: none
// ERROR: 'operator' modifier is inapplicable on this function: must have at least 1 value parameter
fun test() {
class Test{
operator fun get() : Int = 0
}
val test = Test()
test.g<caret>et()
}
@@ -0,0 +1,10 @@
// PROBLEM: none
package p
class C
fun C.get(s: String) = s
fun foo(c: C) {
c.<caret>get("x")
}
@@ -0,0 +1,9 @@
class C {
companion object {
operator fun get(s: String): C = C()
}
}
fun foo() {
C.<caret>get("x")
}
@@ -0,0 +1,9 @@
class C {
companion object {
operator fun get(s: String): C = C()
}
}
fun foo() {
C<caret>["x"]
}
@@ -0,0 +1,9 @@
// PROBLEM: none
// ERROR: Unresolved reference: got
fun test() {
class Test{
operator fun get(i: Int) : Int = 0
}
val test = Test()
test.g<caret>ot(0)
}
@@ -0,0 +1,9 @@
// FIX: Replace 'set' call with indexing operator
class C {
operator fun set(s: String, value: Int) {}
}
fun foo() {
C().<caret>set("x", 1)
}
@@ -0,0 +1,9 @@
// FIX: Replace 'set' call with indexing operator
class C {
operator fun set(s: String, value: Int) {}
}
fun foo() {
C()<caret>["x"] = 1
}
@@ -0,0 +1,11 @@
// FIX: Replace 'set' call with indexing operator
class C {
operator fun set(s: String, p: Int, value: Int): Boolean = true
}
class D(val c: C) {
fun foo() {
this.c.<caret>set("x", 2, 1)
}
}
@@ -0,0 +1,11 @@
// FIX: Replace 'set' call with indexing operator
class C {
operator fun set(s: String, p: Int, value: Int): Boolean = true
}
class D(val c: C) {
fun foo() {
this.c<caret>["x", 2] = 1
}
}
@@ -0,0 +1,11 @@
// PROBLEM: none
class C {
operator fun set(s: String, value: Int): Boolean = true
}
class D(val c: C) {
fun foo(): Boolean {
return this.c.<caret>set("x", 1)
}
}
@@ -0,0 +1,12 @@
// PROBLEM: none
// ERROR: 'operator' modifier is inapplicable on this function: must have at least 2 value parameters
class C {
operator fun set(){}
}
class D(val c: C) {
fun foo() {
this.c.<caret>set()
}
}
@@ -0,0 +1,12 @@
// PROBLEM: none
// ERROR: 'operator' modifier is inapplicable on this function: last parameter should not have a default value or be a vararg
class C {
operator fun set(s: String, vararg value: Int): Boolean = true
}
class D(val c: C) {
fun foo() {
this.c.<caret>set("x", 1, 2)
}
}
@@ -0,0 +1,9 @@
// FIX: Replace 'get' call with indexing operator
fun test() {
class Test{
operator fun get(i: Int) : Int = 0
}
val test = Test()
test.g<caret>et(0)
}
@@ -0,0 +1,9 @@
// FIX: Replace 'get' call with indexing operator
fun test() {
class Test{
operator fun get(i: Int) : Int = 0
}
val test = Test()
test<caret>[0]
}
@@ -0,0 +1,5 @@
class JavaClass {
public static JavaClass get(JavaClass s) {
return s;
}
}
@@ -0,0 +1,4 @@
// PROBLEM: none
fun foo() {
JavaClass.<caret>get(JavaClass())
}
@@ -0,0 +1,11 @@
// PROBLEM: none
open class Base {
open operator fun get(s: String) = ""
}
class C : Base() {
override fun get(s: String): String {
return super.<caret>get(s)
}
}
@@ -0,0 +1,10 @@
// PROBLEM: none
// ERROR: 'operator' modifier is inapplicable on this function: must be a member or an extension function
package p
operator fun get(s: String) = s
fun foo() {
p.<caret>get("x")
}
@@ -0,0 +1,8 @@
// PROBLEM: none
fun test() {
class Test{
operator fun get(a: Int, vararg b: Int, c: Int = 0) : Int = 0
}
val test = Test()
test.g<caret>et(1, 3, 4, c=5)
}
@@ -0,0 +1,8 @@
// PROBLEM: none
fun test() {
class Test{
operator fun get(a: Int = 0, b: Int = 1, c: Int = 2, d: Int = 3) : Int = 0
}
val test = Test()
test.g<caret>et(1, c=3, b=2)
}