Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect fix of null-safety rule #857

Merged
merged 12 commits into from
Apr 30, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.cqfn.diktat.ruleset.utils.*

import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.BLOCK
import com.pinterest.ktlint.core.ast.ElementType.BREAK
import com.pinterest.ktlint.core.ast.ElementType.CONDITION
import com.pinterest.ktlint.core.ast.ElementType.ELSE
import com.pinterest.ktlint.core.ast.ElementType.IF
Expand Down Expand Up @@ -88,6 +90,17 @@ class NullChecksRule(configRules: List<RulesConfig>) : DiktatRule(
binaryExpression: KtBinaryExpression,
isEqualToNull: Boolean) {
val variableName = binaryExpression.left!!.text
listOf(THEN, ELSE).forEach { elementType ->
// `break` word in block should be converted to `return`
condition.treeParent.findChildByType(elementType)?.let { blockNode ->
val correctChild = blockNode.findChildByType(BLOCK) ?: blockNode
correctChild.findChildByType(BREAK)?.let {
val newNode = KotlinParser().createNode("return")
correctChild.addChild(newNode, it)
correctChild.removeChild(it)
}
}
}
val thenCodeLines = condition.extractLinesFromBlock(THEN)
val elseCodeLines = condition.extractLinesFromBlock(ELSE)
val text = if (isEqualToNull) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,26 @@ value
}
}

fun foo() {
var result: Int? = 10
while (result != 0 ) {
result?.let {
goo()
}
?: run {
for(i in 1..10)
break
}
}
while (result != 0) {
result = goo()
result?.let {
goo()
}
?: run {
println(123)
return
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,24 @@ fun test() {
}
}

fun foo() {
var result: Int? = 10
while (result != 0 ) {
if (result != null) {
goo()
} else {
for(i in 1..10)
break
}
}
while (result != 0) {
result = goo()
if (result != null) {
goo()
} else {
println(123)
break
}
}
}