Refactoring: make "replace call with binary operator" an inspection

This commit is contained in:
Mikhail Glukhikh
2017-12-20 16:02:15 +03:00
parent 6d4b5bc48f
commit ada7287c66
69 changed files with 320 additions and 321 deletions
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.conventionNameCalls.ReplaceCallWithBinaryOperatorInspection
@@ -0,0 +1,8 @@
// FIX: Replace with '/' operator
fun test() {
class Test {
operator fun div(a: Int): Test = Test()
}
val test = Test()
test.<caret>div(1)
}
@@ -0,0 +1,8 @@
// FIX: Replace with '/' operator
fun test() {
class Test {
operator fun div(a: Int): Test = Test()
}
val test = Test()
test / 1
}
@@ -0,0 +1,3 @@
// FIX: Replace with '==' operator
val x = 2.<caret>equals(2)
@@ -0,0 +1,3 @@
// FIX: Replace with '==' operator
val x = 2 == 2
@@ -0,0 +1,3 @@
// PROBLEM: none
val x = 2.compareTo<caret>(2) == 0
@@ -0,0 +1,11 @@
// PROBLEM: none
class Foo
fun Foo?.equals(other: Foo?) = true
fun bar(f1: Foo?, f2: Foo?) {
if (f1.equals<caret>(f2)) {
}
}
@@ -0,0 +1,6 @@
fun test() {
class Test()
operator fun Test.div(a: Int): Test = Test()
val test = Test()
test.<caret>div(1)
}
@@ -0,0 +1,6 @@
fun test() {
class Test()
operator fun Test.div(a: Int): Test = Test()
val test = Test()
test / 1
}
@@ -0,0 +1,9 @@
fun test() {
class Test {
operator fun plus(fn: () -> Test): Test = fn()
}
val test = Test()
test.pl<caret>us {
Test()
}
}
@@ -0,0 +1,9 @@
fun test() {
class Test {
operator fun plus(fn: () -> Test): Test = fn()
}
val test = Test()
test + {
Test()
}
}
@@ -0,0 +1,3 @@
// FIX: Replace with '>' operator
val x = 3.compare<caret>To(2) > 0
@@ -0,0 +1,3 @@
// FIX: Replace with '>' operator
val x = 3 > 2
@@ -0,0 +1,3 @@
// FIX: Replace with '<=' operator
val x = 0 >= 4.compare<caret>To(5)
@@ -0,0 +1,3 @@
// FIX: Replace with '<=' operator
val x = 4 <= 5
@@ -0,0 +1,8 @@
// FIX: Replace with '-' operator
fun test() {
class Test {
operator fun minus(a: Int): Test = Test()
}
val test = Test()
test.min<caret>us(1)
}
@@ -0,0 +1,8 @@
// FIX: Replace with '-' operator
fun test() {
class Test {
operator fun minus(a: Int): Test = Test()
}
val test = Test()
test - 1
}
@@ -0,0 +1,9 @@
// PROBLEM: none
// ERROR: 'operator' modifier is inapplicable on this function: must have a single value parameter
fun test() {
class Test{
operator fun plus(a: Int=1, b: Int=2) : Int = 0
}
val test = Test()
test.p<caret>lus(b=3)
}
@@ -0,0 +1,9 @@
// PROBLEM: none
fun test() {
class Test {
operator fun mod(a: Int): Test = Test()
}
val test = Test()
test.<caret>mod(1)
}
@@ -0,0 +1,9 @@
// PROBLEM: none
// ERROR: 'operator' modifier is inapplicable on this function: must have a single value parameter
fun test() {
class Test {
operator fun plus(a: Int, b: Int): Test = Test()
}
val test = Test()
test.pl<caret>us(1, 2)
}
@@ -0,0 +1,3 @@
// FIX: Replace with '!=' operator
val x = !2.eq<caret>uals(3)
@@ -0,0 +1,3 @@
// FIX: Replace with '!=' operator
val x = 2 != 3
@@ -0,0 +1,3 @@
// FIX: Replace with '!=' operator
val x = !(2.<caret>equals(3))
@@ -0,0 +1,3 @@
// FIX: Replace with '!=' operator
val x = 2 != 3
@@ -0,0 +1,3 @@
// FIX: Replace with '==' operator
val x = !(2.equ<caret>als(3) && 4.equals(5))
@@ -0,0 +1,3 @@
// FIX: Replace with '==' operator
val x = !(2 == 3 && 4.equals(5))
@@ -0,0 +1,8 @@
// FIX: Replace with '+' operator
fun test() {
class Test {
operator fun plus(a: Int): Test = Test()
}
val test = Test()
test.pl<caret>us(1)
}
@@ -0,0 +1,8 @@
// FIX: Replace with '+' operator
fun test() {
class Test {
operator fun plus(a: Int): Test = Test()
}
val test = Test()
test + 1
}
@@ -0,0 +1,9 @@
class C {
companion object {
operator fun plus(s: String): C = C()
}
}
fun foo() {
C.<caret>plus("x")
}
@@ -0,0 +1,9 @@
class C {
companion object {
operator fun plus(s: String): C = C()
}
}
fun foo() {
C + "x"
}
@@ -0,0 +1,8 @@
// FIX: Replace with '..' operator
fun test() {
class Test {
operator fun rangeTo(a: Int): Test = Test()
}
val test = Test()
test.range<caret>To(1)
}
@@ -0,0 +1,8 @@
// FIX: Replace with '..' operator
fun test() {
class Test {
operator fun rangeTo(a: Int): Test = Test()
}
val test = Test()
test..1
}
@@ -0,0 +1,9 @@
// FIX: Replace with '%' operator
fun test() {
class Test {
operator fun rem(a: Int): Test = Test()
}
val test = Test()
test.<caret>rem(1)
}
@@ -0,0 +1,9 @@
// FIX: Replace with '%' operator
fun test() {
class Test {
operator fun rem(a: Int): Test = Test()
}
val test = Test()
test % 1
}
@@ -0,0 +1,5 @@
// PROBLEM: none
// ERROR: Operator call corresponds to a dot-qualified call 'nullable?.compareTo(1).compareTo(0)' which is not allowed on a nullable receiver 'nullable?.compareTo(1)'.
val nullable: Int? = null
val x = nullable?.compareTo<caret>(1) >= 0
@@ -0,0 +1,11 @@
// PROBLEM: none
open class Base {
open operator fun plus(s: String) = ""
}
class C : Base() {
override fun plus(s: String): String {
return super.<caret>plus(s)
}
}
@@ -0,0 +1,8 @@
// FIX: Replace with '*' operator
fun test() {
class Test {
operator fun times(a: Int): Test = Test()
}
val test = Test()
test.time<caret>s(1)
}
@@ -0,0 +1,8 @@
// FIX: Replace with '*' operator
fun test() {
class Test {
operator fun times(a: Int): Test = Test()
}
val test = Test()
test * 1
}
@@ -0,0 +1,8 @@
// PROBLEM: none
fun test() {
class Test {
operator fun <T> div(a: Test): T? = a as? T
}
val test = Test()
test.div<caret><Int>(Test())
}
@@ -0,0 +1,9 @@
// PROBLEM: none
// ERROR: 'operator' modifier is inapplicable on this function: must have a single value parameter
fun test() {
class Test{
operator fun plus(vararg b: Int, c: Int = 0): Int = 0
}
val test = Test()
test.plus<caret>(c=5)
}
@@ -0,0 +1,9 @@
// PROBLEM: none
// ERROR: 'operator' modifier is inapplicable on this function: must have a single value parameter
fun test() {
class Test{
operator fun plus(vararg b: Int, c: Int = 0): Int = 0
}
val test = Test()
test.plus<caret>(0, 1)
}
@@ -0,0 +1,7 @@
fun test() {
class Test {
operator fun plus(a: Int): Test = Test()
}
val test = Test()
test.pl<caret>us(a=1)
}
@@ -0,0 +1,7 @@
fun test() {
class Test {
operator fun plus(a: Int): Test = Test()
}
val test = Test()
test + 1
}