"if-then to safe access": treat transformation to 'let' as intention

Related to KT-7675
This commit is contained in:
Mikhail Glukhikh
2018-08-15 12:32:51 +03:00
parent f5cfec4a91
commit 5b77def0e0
10 changed files with 20 additions and 41 deletions
@@ -289,15 +289,22 @@ internal fun KtIfExpression.buildSelectTransformationData(): IfThenToSelectData?
return IfThenToSelectData(context, condition, receiverExpression, baseClause, negatedClause)
}
internal fun KtExpression?.isClauseTransformableToLetOnly() =
this is KtCallExpression && resolveToCall()?.getImplicitReceiverValue() == null
internal fun KtIfExpression.shouldBeTransformed(): Boolean {
val condition = condition
return when (condition) {
is KtBinaryExpression -> true
is KtBinaryExpression -> {
val baseClause = (if (condition.operationToken == KtTokens.EQEQ) `else` else then)?.unwrapBlockOrParenthesis()
!baseClause.isClauseTransformableToLetOnly()
}
is KtIsExpression -> {
if (!isMultiLine()) true
else {
val baseClause = (if (condition.isNegated) `else` else then)?.unwrapBlockOrParenthesis()
baseClause !is KtDotQualifiedExpression
val baseClause = (if (condition.isNegated) `else` else then)?.unwrapBlockOrParenthesis()
when {
baseClause.isClauseTransformableToLetOnly() -> false
!isMultiLine() -> true
else -> baseClause !is KtDotQualifiedExpression
}
}
else -> false
@@ -1,4 +1,5 @@
// WITH_RUNTIME
// HIGHLIGHT: INFORMATION
fun convert(x: String, y: String) = ""
fun foo(a: String?, b: String) {
@@ -1,4 +1,5 @@
// WITH_RUNTIME
// HIGHLIGHT: INFORMATION
fun convert(x: String, y: String) = ""
fun foo(a: String?, b: String) {
@@ -1,4 +1,5 @@
// WITH_RUNTIME
// HIGHLIGHT: INFORMATION
fun convert(x: String, y: String) = ""
fun foo(a: Any?, b: String) {
@@ -1,4 +1,5 @@
// WITH_RUNTIME
// HIGHLIGHT: INFORMATION
fun convert(x: String, y: String) = ""
fun foo(a: Any?, b: String) {
@@ -1,4 +1,5 @@
// WITH_RUNTIME
// HIGHLIGHT: INFORMATION
fun convert(x: String, y: Int) = ""
fun foo(a: String?, it: Int) {
@@ -1,4 +1,5 @@
// WITH_RUNTIME
// HIGHLIGHT: INFORMATION
fun convert(x: String, y: Int) = ""
fun foo(a: String?, it: Int) {
@@ -1,4 +1,5 @@
// WITH_RUNTIME
// HIGHLIGHT: INFORMATION
fun maybeFoo(): String? {
return "foo"
}
@@ -1,4 +1,5 @@
// WITH_RUNTIME
// HIGHLIGHT: INFORMATION
fun maybeFoo(): String? {
return "foo"
}
@@ -167,40 +167,4 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Foldable if-then</description>
</problem>
<problem>
<file>call4.kt</file>
<line>10</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</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>&lt;default&gt;</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>&lt;default&gt;</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>&lt;default&gt;</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>