"if-then to safe access": treat transformation to 'let' as intention
Related to KT-7675
This commit is contained in:
+12
-5
@@ -289,15 +289,22 @@ internal fun KtIfExpression.buildSelectTransformationData(): IfThenToSelectData?
|
|||||||
return IfThenToSelectData(context, condition, receiverExpression, baseClause, negatedClause)
|
return IfThenToSelectData(context, condition, receiverExpression, baseClause, negatedClause)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal fun KtExpression?.isClauseTransformableToLetOnly() =
|
||||||
|
this is KtCallExpression && resolveToCall()?.getImplicitReceiverValue() == null
|
||||||
|
|
||||||
internal fun KtIfExpression.shouldBeTransformed(): Boolean {
|
internal fun KtIfExpression.shouldBeTransformed(): Boolean {
|
||||||
val condition = condition
|
val condition = condition
|
||||||
return when (condition) {
|
return when (condition) {
|
||||||
is KtBinaryExpression -> true
|
is KtBinaryExpression -> {
|
||||||
|
val baseClause = (if (condition.operationToken == KtTokens.EQEQ) `else` else then)?.unwrapBlockOrParenthesis()
|
||||||
|
!baseClause.isClauseTransformableToLetOnly()
|
||||||
|
}
|
||||||
is KtIsExpression -> {
|
is KtIsExpression -> {
|
||||||
if (!isMultiLine()) true
|
val baseClause = (if (condition.isNegated) `else` else then)?.unwrapBlockOrParenthesis()
|
||||||
else {
|
when {
|
||||||
val baseClause = (if (condition.isNegated) `else` else then)?.unwrapBlockOrParenthesis()
|
baseClause.isClauseTransformableToLetOnly() -> false
|
||||||
baseClause !is KtDotQualifiedExpression
|
!isMultiLine() -> true
|
||||||
|
else -> baseClause !is KtDotQualifiedExpression
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> false
|
else -> false
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
fun convert(x: String, y: String) = ""
|
fun convert(x: String, y: String) = ""
|
||||||
|
|
||||||
fun foo(a: String?, b: String) {
|
fun foo(a: String?, b: String) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
fun convert(x: String, y: String) = ""
|
fun convert(x: String, y: String) = ""
|
||||||
|
|
||||||
fun foo(a: String?, b: String) {
|
fun foo(a: String?, b: String) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
fun convert(x: String, y: String) = ""
|
fun convert(x: String, y: String) = ""
|
||||||
|
|
||||||
fun foo(a: Any?, b: String) {
|
fun foo(a: Any?, b: String) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
fun convert(x: String, y: String) = ""
|
fun convert(x: String, y: String) = ""
|
||||||
|
|
||||||
fun foo(a: Any?, b: String) {
|
fun foo(a: Any?, b: String) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
fun convert(x: String, y: Int) = ""
|
fun convert(x: String, y: Int) = ""
|
||||||
|
|
||||||
fun foo(a: String?, it: Int) {
|
fun foo(a: String?, it: Int) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
fun convert(x: String, y: Int) = ""
|
fun convert(x: String, y: Int) = ""
|
||||||
|
|
||||||
fun foo(a: String?, it: Int) {
|
fun foo(a: String?, it: Int) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
fun maybeFoo(): String? {
|
fun maybeFoo(): String? {
|
||||||
return "foo"
|
return "foo"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
// HIGHLIGHT: INFORMATION
|
||||||
fun maybeFoo(): String? {
|
fun maybeFoo(): String? {
|
||||||
return "foo"
|
return "foo"
|
||||||
}
|
}
|
||||||
|
|||||||
-36
@@ -167,40 +167,4 @@
|
|||||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
|
||||||
<description>Foldable if-then</description>
|
<description>Foldable if-then</description>
|
||||||
</problem>
|
</problem>
|
||||||
<problem>
|
|
||||||
<file>call4.kt</file>
|
|
||||||
<line>10</line>
|
|
||||||
<module>light_idea_test_case</module>
|
|
||||||
<package><default></package>
|
|
||||||
<entry_point TYPE="file" FQNAME="temp:///src/call4.kt" />
|
|
||||||
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
|
|
||||||
<description>Foldable if-then</description>
|
|
||||||
</problem>
|
|
||||||
<problem>
|
|
||||||
<file>call3.kt</file>
|
|
||||||
<line>5</line>
|
|
||||||
<module>light_idea_test_case</module>
|
|
||||||
<package><default></package>
|
|
||||||
<entry_point TYPE="file" FQNAME="temp:///src/call3.kt" />
|
|
||||||
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
|
|
||||||
<description>Foldable if-then</description>
|
|
||||||
</problem>
|
|
||||||
<problem>
|
|
||||||
<file>call2.kt</file>
|
|
||||||
<line>5</line>
|
|
||||||
<module>light_idea_test_case</module>
|
|
||||||
<package><default></package>
|
|
||||||
<entry_point TYPE="file" FQNAME="temp:///src/call2.kt" />
|
|
||||||
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
|
|
||||||
<description>Foldable if-then</description>
|
|
||||||
</problem>
|
|
||||||
<problem>
|
|
||||||
<file>call.kt</file>
|
|
||||||
<line>5</line>
|
|
||||||
<module>light_idea_test_case</module>
|
|
||||||
<package><default></package>
|
|
||||||
<entry_point TYPE="file" FQNAME="temp:///src/call.kt" />
|
|
||||||
<problem_class severity="INFO" attribute_key="INFO_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
|
|
||||||
<description>Foldable if-then</description>
|
|
||||||
</problem>
|
|
||||||
</problems>
|
</problems>
|
||||||
|
|||||||
Reference in New Issue
Block a user