if-then to safe access: report even if condition in parentheses #KT-26662 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-09-10 13:13:57 +09:00
committed by Mikhail Glukhikh
parent 6d9fb4f382
commit b5f73ebd0f
4 changed files with 23 additions and 2 deletions
@@ -277,10 +277,10 @@ data class IfThenToSelectData(
internal fun KtIfExpression.buildSelectTransformationData(): IfThenToSelectData? {
val context = analyze()
val condition = condition as? KtOperationExpression ?: return null
val condition = condition?.unwrapBlockOrParenthesis() as? KtOperationExpression ?: return null
val thenClause = then?.unwrapBlockOrParenthesis()
val elseClause = `else`?.unwrapBlockOrParenthesis()
val receiverExpression = condition.checkedExpression() ?: return null
val receiverExpression = condition.checkedExpression()?.unwrapBlockOrParenthesis() ?: return null
val (baseClause, negatedClause) = when (condition) {
is KtBinaryExpression -> when (condition.operationToken) {
@@ -0,0 +1,9 @@
class Some {
fun bar() {}
}
fun Some?.foo() {
<caret>if (((this) != null)) {
bar()
}
}
@@ -0,0 +1,7 @@
class Some {
fun bar() {}
}
fun Some?.foo() {
this?.bar()
}
@@ -106,6 +106,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionComparesNullWithNull.kt");
}
@TestMetadata("conditionInParentheses.kt")
public void testConditionInParentheses() throws Exception {
runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionInParentheses.kt");
}
@TestMetadata("conditionInvalidBinaryExp.kt")
public void testConditionInvalidBinaryExp() throws Exception {
runTest("idea/testData/inspectionsLocal/branched/ifThenToSafeAccess/conditionInvalidBinaryExp.kt");