Convert IfThenToElvisIntention to inspection & decrease severity to INFO

#KT-16067 Fixed
This commit is contained in:
Dmitry Gridin
2019-06-03 20:27:35 +07:00
parent d8f32b1a0c
commit f2accb7b9e
101 changed files with 498 additions and 487 deletions
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.branchedTransformations.IfThenToElvisInspection
@@ -0,0 +1,13 @@
// WITH_RUNTIME
fun maybeFoo(): String? {
return "foo"
}
fun test(): String? {
var foo = maybeFoo()
val bar = if (foo == null<caret>)
"hello"
else
foo
return foo
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
fun maybeFoo(): String? {
return "foo"
}
fun test(): String? {
var foo = maybeFoo()
val bar = foo ?: "hello"
return foo
}
@@ -0,0 +1,15 @@
// PROBLEM: none
fun <T> doSomething(a: T) {}
fun main(args: Array<String>) {
val foo: String? = null
val bar = "bar"
if (foo != null<caret>) {
doSomething ("Hello")
foo
}
else {
bar
}
}
@@ -0,0 +1,14 @@
// PROBLEM: none
fun main(args: Array<String>) {
val foo = null
val a = "a"
val b = "b"
if (foo != null<caret>) {
a
}
else {
b
}
}
@@ -0,0 +1,9 @@
// See KT-15227
class My(val local: Boolean)
class Your(val my: My?, val parent: Any?)
fun foo(your: Your): Boolean {
val my = your.my
return <caret>if (my != null) my.local else your.parent != null
}
@@ -0,0 +1,9 @@
// See KT-15227
class My(val local: Boolean)
class Your(val my: My?, val parent: Any?)
fun foo(your: Your): Boolean {
val my = your.my
return my?.local ?: (your.parent != null)
}
@@ -0,0 +1,10 @@
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
if (null == <caret>null) {
foo
}
else {
null
}
}
@@ -0,0 +1,13 @@
// PROBLEM: none
operator fun <T> T.compareTo(a: T): Int = 0
fun main(args: Array<String>) {
val foo = null
val bar = "bar"
if (foo > null<caret>) {
foo
}
else {
bar
}
}
@@ -0,0 +1,15 @@
// PROBLEM: none
// ERROR: Type mismatch: inferred type is Int but Boolean was expected
// ERROR: Type mismatch: inferred type is Int but Boolean was expected
// ERROR: Operator call corresponds to a dot-qualified call 'foo.times(10)' which is not allowed on a nullable receiver 'foo'.
fun main(args: Array<String>) {
val foo: Int? = 4
val bar = 3
if (foo * 10<caret>) {
foo
}
else {
bar
}
}
@@ -0,0 +1,8 @@
// "Replace 'if' expression with elvis expression" "true"
data class IntPair(val first: Int?, val second: Int?)
fun f(pair: IntPair): Int {
val (x, y) = pair
return if (x != <caret>null) x else 5
}
@@ -0,0 +1,8 @@
// "Replace 'if' expression with elvis expression" "true"
data class IntPair(val first: Int?, val second: Int?)
fun f(pair: IntPair): Int {
val (x, y) = pair
return x ?: 5
}
@@ -0,0 +1,17 @@
fun <T> doSomething(a: T) {}
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val foo = maybeFoo()
doSomething(foo)
val bar = "bar"
if (foo != null<caret>) {
foo
}
else {
bar
}
}
@@ -0,0 +1,12 @@
fun <T> doSomething(a: T) {}
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val foo = maybeFoo()
doSomething(foo)
val bar = "bar"
foo ?: bar
}
@@ -0,0 +1,13 @@
fun maybeFoo(): String? {
return "foo"
}
val x = maybeFoo()
fun main(args: Array<String>) {
if (x !=<caret> null) {
x
} else {
"abc"
}
}
@@ -0,0 +1,9 @@
fun maybeFoo(): String? {
return "foo"
}
val x = maybeFoo()
fun main(args: Array<String>) {
x ?: "abc"
}
@@ -0,0 +1,12 @@
// PROBLEM: none
fun main(args: Array<String>) {
val foo = "foo"
val bar = "foo"
if (<caret>) {
foo
}
else {
bar
}
}
@@ -0,0 +1,10 @@
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = null
if (foo != null<caret>) {
foo
}
else {
}
}
@@ -0,0 +1,10 @@
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
val bar = "bar"
if (foo == null<caret>) {
}
else {
bar
}
}
@@ -0,0 +1,5 @@
class Foo {
fun Foo.bar(): Int = 1
}
fun Foo.test(foo: Foo?): Int = <caret>if (foo == null) { 0 } else { foo.bar() }
@@ -0,0 +1,5 @@
class Foo {
fun Foo.bar(): Int = 1
}
fun Foo.test(foo: Foo?): Int = foo?.bar() ?: 0
@@ -0,0 +1,14 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val foo = maybeFoo()
val bar = "bar"
if (foo != null<caret>) {
foo
}
else {
bar
}
}
@@ -0,0 +1,8 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val bar = "bar"
maybeFoo() ?: bar
}
@@ -0,0 +1,12 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val foo = maybeFoo()
val bar = "bar"
if (foo != null<caret>)
foo
else
bar
}
@@ -0,0 +1,8 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val bar = "bar"
maybeFoo() ?: bar
}
@@ -0,0 +1,14 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val foo = maybeFoo()
val bar = "bar"
val x = if (foo == null<caret>) {
bar
}
else {
foo
}
}
@@ -0,0 +1,8 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val bar = "bar"
val x = maybeFoo() ?: bar
}
@@ -0,0 +1,9 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val foo = maybeFoo()
val bar = "bar"
val x = "baz" + if (foo == null<caret>) bar else foo
}
@@ -0,0 +1,8 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val bar = "bar"
val x = "baz" + (maybeFoo() ?: bar)
}
@@ -0,0 +1,6 @@
fun foo(arg: Any): Int {
// 1
return <caret>if (arg is Int) arg
// 2
else 10
}
@@ -0,0 +1,6 @@
fun foo(arg: Any): Int {
// 1
return arg as? Int
// 2
?: 10
}
@@ -0,0 +1,2 @@
// WITH_RUNTIME
fun String?.foo() = <caret>if (this == null) true else isEmpty()
@@ -0,0 +1,2 @@
// WITH_RUNTIME
fun String?.foo() = this?.isEmpty() ?: true
@@ -0,0 +1,101 @@
<problems>
<problem>
<file>ifAsPartOfExpression.kt</file>
<line>8</line>
<module>light_idea_test_case</module>
<package>ifAsPartOfExpression</package>
<entry_point TYPE="method" FQNAME="ifAsPartOfExpression.IfAsPartOfExpressionKt void main(java.lang.String[] args)" />
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>If-Then foldable to '?:'</description>
</problem>
<problem>
<file>comparisonInElse.kt</file>
<line>8</line>
<module>light_idea_test_case</module>
<package>comparisonInElse</package>
<entry_point TYPE="method" FQNAME="comparisonInElse.ComparisonInElseKt boolean foo(comparisonInElse.Your your)" />
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>If-Then foldable to '?:'</description>
</problem>
<problem>
<file>willNotInlineClassProperty.kt</file>
<line>3</line>
<module>light_idea_test_case</module>
<package>willNotInlineClassProperty</package>
<entry_point TYPE="field" FQNAME="willNotInlineClassProperty.F c" />
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>If-Then foldable to '?:'</description>
</problem>
<problem>
<file>applicableForLocalStableVar.kt</file>
<line>8</line>
<module>light_idea_test_case</module>
<package>applicableForLocalStableVar</package>
<entry_point TYPE="method" FQNAME="applicableForLocalStableVar.ApplicableForLocalStableVarKt java.lang.String test()" />
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>If-Then foldable to '?:'</description>
</problem>
<problem>
<file>nullCheckWithSelector.kt</file>
<line>4</line>
<module>light_idea_test_case</module>
<package>nullCheckWithSelector</package>
<entry_point TYPE="method" FQNAME="nullCheckWithSelector.NullCheckWithSelectorKt int foo(nullCheckWithSelector.My arg)" />
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>If-Then foldable to '?:'</description>
</problem>
<problem>
<file>nullCheckWithSelectorCall.kt</file>
<line>5</line>
<module>light_idea_test_case</module>
<package>nullCheckWithSelectorCall</package>
<entry_point TYPE="field" FQNAME="nullCheckWithSelectorCall.NullCheckWithSelectorCallKt foo" />
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>If-Then foldable to '?:'</description>
</problem>
<problem>
<file>implicitReceiver.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<package>implicitReceiver</package>
<entry_point TYPE="method" FQNAME="implicitReceiver.ImplicitReceiverKt boolean foo(java.lang.String $self)" />
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>If-Then foldable to '?:'</description>
</problem>
<problem>
<file>nullCheckWithSelectorCallChain.kt</file>
<line>5</line>
<module>light_idea_test_case</module>
<package>nullCheckWithSelectorCallChain</package>
<entry_point TYPE="field" FQNAME="nullCheckWithSelectorCallChain.NullCheckWithSelectorCallChainKt foo" />
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>If-Then foldable to '?:'</description>
</problem>
<problem>
<file>extensionFunctionInClass.kt</file>
<line>5</line>
<module>light_idea_test_case</module>
<package>extensionFunctionInClass</package>
<entry_point TYPE="method" FQNAME="extensionFunctionInClass.ExtensionFunctionInClassKt int test(extensionFunctionInClass.Foo $self, extensionFunctionInClass.Foo foo)" />
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>If-Then foldable to '?:'</description>
</problem>
<problem>
<file>doesNotInlineVariableInMultiDeclaration.kt</file>
<line>7</line>
<module>light_idea_test_case</module>
<package>doesNotInlineVariableInMultiDeclaration</package>
<entry_point TYPE="method" FQNAME="doesNotInlineVariableInMultiDeclaration.DoesNotInlineVariableInMultiDeclarationKt int f(doesNotInlineVariableInMultiDeclaration.IntPair pair)" />
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>If-Then foldable to '?:'</description>
</problem>
<problem>
<file>ifAsExpression.kt</file>
<line>8</line>
<module>light_idea_test_case</module>
<package>ifAsExpression</package>
<entry_point TYPE="method" FQNAME="ifAsExpression.IfAsExpressionKt void main(java.lang.String[] args)" />
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>If-Then foldable to '?:'</description>
</problem>
</problems>
@@ -0,0 +1,2 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.branchedTransformations.IfThenToElvisInspection
// WITH_RUNTIME
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): My {
return if (<caret>arg is My) arg else My(42)
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): My {
return arg as? My ?: My(42)
}
@@ -0,0 +1,10 @@
// PROBLEM: none
fun foo(x: CharSequence?) {
val y = if (x is String?) {
x
}
else {
(x as CharSequence).toString()
}<caret>
}
@@ -0,0 +1,12 @@
// PROBLEM: none
interface A
interface B
fun prepare(x: A) = x
fun use(x: A) {}
fun test(x: A) {
val xx = <caret>if (x is B) x else prepare(x)
use(xx)
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): Int {
return if (<caret>arg is My) arg.x else 42
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): Int {
return (arg as? My)?.x ?: 42
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): Int {
return if (<caret>arg is My) arg.x.hashCode() else 42
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): Int {
return (arg as? My)?.x?.hashCode() ?: 42
}
@@ -0,0 +1,13 @@
// WITH_RUNTIME
interface Taggable {
val tag: String
}
fun Any.log() {
val tag = <caret>if (this is Taggable) {
tag
}
else {
this::class.java.simpleName
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
interface Taggable {
val tag: String
}
fun Any.log() {
val tag = (this as? Taggable)?.tag ?: this::class.java.simpleName
}
@@ -0,0 +1,12 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val foo = maybeFoo()
val bar = "bar"
if (foo == null<caret>)
bar
else
foo
}
@@ -0,0 +1,8 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val bar = "bar"
maybeFoo() ?: bar
}
@@ -0,0 +1,12 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val foo = maybeFoo()
val bar = "bar"
if (foo != null<caret>)
foo
else
bar
}
@@ -0,0 +1,8 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val bar = "bar"
maybeFoo() ?: bar
}
@@ -0,0 +1,7 @@
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
if (foo == null<caret>) {
null
}
}
@@ -0,0 +1,9 @@
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
val bar = "bar"
if (foo != null<caret>)
else {
bar
}
}
@@ -0,0 +1,8 @@
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
val bar = "bar"
if<caret> {
foo
} else bar
}
@@ -0,0 +1,11 @@
// PROBLEM: none
fun main(args: Array<String>) {
val foo = "foo"
val bar = "bar"
if (foo == bar<caret>) {
foo
}
else {
bar
}
}
@@ -0,0 +1,11 @@
// PROBLEM: none
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
if (maybeFoo() == null<caret>)
maybeFoo()
else
"bar"
}
@@ -0,0 +1,22 @@
// WITH_RUNTIME
// PROBLEM: none
fun maybeFoo(): String? {
return "foo"
}
fun capture(block: () -> Unit): Unit = Unit
fun test(): String? {
var foo = maybeFoo()
capture {
foo = null
}
val bar = if (foo == null<caret>)
42
else
foo
return foo
}
@@ -0,0 +1,6 @@
// PROBLEM: none
// WITH_RUNTIME
fun main(args: Array<String>) {
val t: String? = "abc"
if (t != null<caret>) t else throw KotlinNullPointerException()
}
@@ -0,0 +1,6 @@
// PROBLEM: none
// WITH_RUNTIME
fun main(args: Array<String>) {
val t: String? = "abc"
if (t == null<caret>) throw NullPointerException() else t
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): My {
return if (<caret>arg !is My) My(42) else arg
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): My {
return arg as? My ?: My(42)
}
@@ -0,0 +1,10 @@
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
if (foo == null<caret>) {
null
}
else {
foo
}
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: My?): Int {
return if (<caret>arg != null) arg.x else 42
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: My?): Int {
return arg?.x ?: 42
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
val nullableString: String? = "abc"
val foo = if (<caret>nullableString != null) {
nullableString.toUpperCase()
} else {
""
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
val nullableString: String? = "abc"
val foo = nullableString?.toUpperCase() ?: ""
@@ -0,0 +1,9 @@
// WITH_RUNTIME
val nullableString: String? = "abc"
val foo = if (<caret>nullableString != null) {
nullableString.toUpperCase().toLowerCase()
} else {
""
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
val nullableString: String? = "abc"
val foo = nullableString?.toUpperCase()?.toLowerCase() ?: ""
@@ -0,0 +1,7 @@
// PROBLEM: none
class Something {
fun nullable(): Int? = null
}
fun Something?.nullable(value: Int): Int? =
<caret>if (this == null) value else nullable()
@@ -0,0 +1,7 @@
// PROBLEM: none
fun foo(p: String?): String? {
return <caret>if (p != null) p.bar() else "a"
}
fun String.bar(): String? = null
@@ -0,0 +1,15 @@
// PROBLEM: none
fun <T> doSomething(a: T) {}
fun main(args: Array<String>) {
val foo: String? = null
val bar = "bar"
if (foo != null<caret>) {
foo
}
else {
doSomething("Hello")
bar
}
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
fun foo(s: String?) {
val x = <caret>if (s != null) {
bar(s)
}
else {
13
}
}
fun bar(s: String): Int = 42
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun foo(s: String?) {
val x = s?.let { bar(it) } ?: 13
}
fun bar(s: String): Int = 42
@@ -0,0 +1,12 @@
// WITH_RUNTIME
fun foo(it: String?) {
val x = <caret>if (it != null) {
bar(it)
}
else {
13
}
}
fun bar(s: String): Int = 42
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun foo(it: String?) {
val x = it?.let { it1 -> bar(it1) } ?: 13
}
fun bar(s: String): Int = 42
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class Test {
fun doAThing(param1: String): String {
return param1
}
fun doAThingIfPresent(param1: String?): String {
return <caret>if (param1 != null) {
doAThing(param1)
} else {
""
}
}
}
@@ -0,0 +1,11 @@
// WITH_RUNTIME
class Test {
fun doAThing(param1: String): String {
return param1
}
fun doAThingIfPresent(param1: String?): String {
return param1?.let { doAThing(it) } ?: ""
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class Test {
fun doAThing(param1: String): String {
return param1
}
fun doAThingIfPresent(param1: Any?): String {
return <caret>if (param1 is String) {
doAThing(param1)
} else {
""
}
}
}
@@ -0,0 +1,11 @@
// WITH_RUNTIME
class Test {
fun doAThing(param1: String): String {
return param1
}
fun doAThingIfPresent(param1: Any?): String {
return (param1 as? String)?.let { doAThing(it) } ?: ""
}
}
@@ -0,0 +1,12 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val bar = "bar"
val foo = maybeFoo()
if (null == foo<caret>)
bar
else
foo
}
@@ -0,0 +1,8 @@
fun maybeFoo(): String? {
return "foo"
}
fun main(args: Array<String>) {
val bar = "bar"
maybeFoo() ?: bar
}
@@ -0,0 +1,13 @@
fun maybeFoo(): String? {
return "foo"
}
fun bar(): String = "bar"
fun main(args: Array<String>) {
val foo = maybeFoo()
if (null != foo<caret>)
foo
else
bar()
}
@@ -0,0 +1,9 @@
fun maybeFoo(): String? {
return "foo"
}
fun bar(): String = "bar"
fun main(args: Array<String>) {
maybeFoo() ?: bar()
}
@@ -0,0 +1,10 @@
// PROBLEM: none
fun main(args: Array<String>) {
val foo = null
if (foo == null<caret>) {
null
}
else {
null
}
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun main(args: Array<String>) {
val t: String? = "abc"
if (t != null<caret>) t else throw NullPointerException("'t' must not be null")
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun main(args: Array<String>) {
"abc" ?: throw NullPointerException("'t' must not be null")
}
@@ -0,0 +1,16 @@
// PROBLEM: none
open class Some {
fun bar() {}
}
object Obj : Some()
fun foo(arg: Any?) {
<caret>if (arg is Some) {
arg.bar()
}
else {
Obj.bar()
}
}
@@ -0,0 +1,8 @@
class F(a: Int?) {
val b = a
val c = if (b !=<caret> null) b else 2
}
fun main(args: Array<String>) {
F(1).c
}
@@ -0,0 +1,8 @@
class F(a: Int?) {
val b = a
val c = b ?: 2
}
fun main(args: Array<String>) {
F(1).c
}