Skip to content

Commit

Permalink
Incorrect fix of null-safety rule
Browse files Browse the repository at this point in the history
### What's done:
Fixed logic and added more tests
  • Loading branch information
kentr0w committed Apr 30, 2021
1 parent 5658521 commit 91cfffb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class NullChecksRule(configRules: List<RulesConfig>) : DiktatRule(
astNode.hasChildOfType(BREAK) &&
(astNode.psi as? KtBlockExpression)?.statements?.size != 1
} ?: false
return !(isBlockInIfWithBreak || isOneLineBlockInIfWithBreak)
return (!isBlockInIfWithBreak && !isOneLineBlockInIfWithBreak)
}

@Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION")
Expand All @@ -126,7 +126,7 @@ class NullChecksRule(configRules: List<RulesConfig>) : DiktatRule(
if (condition.getBreakNodeFromIf(THEN)) {
"$variableName ?: break"
} else {
"$variableName ?: run {\n${thenCodeLines?.joinToString(separator = "\n")}\n}"
"$variableName ?: run {${thenCodeLines?.joinToString(prefix = "\n" , postfix = "\n", separator = "\n")}}"
}
thenCodeLines!!.singleOrNull() == "null" -> """
|$variableName?.let {
Expand All @@ -150,9 +150,9 @@ class NullChecksRule(configRules: List<RulesConfig>) : DiktatRule(
} else {
when {
elseCodeLines.isNullOrEmpty() || (elseCodeLines.singleOrNull() == "null") ->
"$variableName?.let {\n${thenCodeLines?.joinToString(separator = "\n")}\n}"
"$variableName?.let {${thenCodeLines?.joinToString(prefix = "\n" , postfix = "\n", separator = "\n")}}"
elseCodeLines.singleOrNull() == "break" ->
"$variableName?.let {\n${thenCodeLines?.joinToString(separator = "\n")}\n} ?: break"
"$variableName?.let {${thenCodeLines?.joinToString(prefix = "\n" , postfix = "\n", separator = "\n")}} ?: break"
else -> """
|$variableName?.let {
|${thenCodeLines?.joinToString(separator = "\n")}
Expand Down Expand Up @@ -203,9 +203,10 @@ class NullChecksRule(configRules: List<RulesConfig>) : DiktatRule(

private fun ASTNode.getBreakNodeFromIf(type: IElementType) = this
.treeParent
.findChildByType(type)?.let {
it.findChildByType(BLOCK) ?: it
}?.hasChildOfType(BREAK) ?: false
.findChildByType(type)
?.let { it.findChildByType(BLOCK) ?: it }
?.findAllNodesWithCondition({it.elementType == BREAK})?.isNotEmpty()
?: false

private fun ASTNode.extractLinesFromBlock(type: IElementType): List<String>? =
treeParent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,28 @@ fun foo() {
}
}

fun foo() {
var result: Int? = 19
while(result != 0 ) {
if (result != null) {
foo()
break
} else {
break
}
}
}

fun foo() {
var result: Int? = 19
while(result != 0 ) {
if (result != null) {
result?.let {
goo()
} ?: break
} else {
break
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,30 @@ fun foo() {
}
}

fun foo() {
var result: Int? = 19
while(result != 0 ) {
if (result != null) {
foo()
break
} else {
break
}
}
}

fun foo() {
var result: Int? = 19
while(result != 0 ) {
if (result != null) {
if (result != null) {
goo()
} else {
break
}
} else {
break
}
}
}

0 comments on commit 91cfffb

Please sign in to comment.