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

Bugfix for fix mode of AVOID_NULL_CHECK #1535

Merged
merged 6 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ class NullChecksRule(configRules: List<RulesConfig>) : DiktatRule(

val text = "$thenEditedCodeLines $elseEditedCodeLines"
val tree = KotlinParser().createNode(text)
condition.treeParent.treeParent.addChild(tree, condition.treeParent)
condition.treeParent.treeParent.removeChild(condition.treeParent)
val ifNode = condition.treeParent
ifNode.treeParent.replaceChild(ifNode, tree)
}

private fun getEditedElseCodeLines(elseCodeLines: List<String>?, numberOfStatementsInElseBlock: Int): String = when {
// else { "null"/empty } -> ""
elseCodeLines == null || elseCodeLines.singleOrNull() == "null" -> ""
// else { bar() } -> ?: bar()
numberOfStatementsInElseBlock == 1 -> "?: ${elseCodeLines.joinToString(postfix = "\n", separator = "\n")}"
numberOfStatementsInElseBlock == 1 && elseCodeLines.singleOrNull()?.hasAssignment() != true -> "?: ${elseCodeLines.joinToString(postfix = "\n", separator = "\n")}"
// else { ... } -> ?: run { ... }
else -> getDefaultCaseElseCodeLines(elseCodeLines)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal const val SPACE = ' '

internal const val TAB = '\t'

private const val REGEX_FOR_ASSIGNMENT_DETECTION = "[^=]+=[^=]+"

@Suppress("VARIABLE_NAME_INCORRECT_FORMAT")
val JAVA = arrayOf("abstract", "assert", "boolean",
"break", "byte", "case", "catch", "char", "class", "const",
Expand All @@ -37,6 +39,11 @@ val KOTLIN = KtTokens.KEYWORDS
*/
val loggerPropertyRegex = "(?iu)^log(?:ger)?$".toRegex()

/**
* @return true if this String includes assignment operator, false otherwise
*/
fun String.hasAssignment(): Boolean = contains(REGEX_FOR_ASSIGNMENT_DETECTION.toRegex())
petertrr marked this conversation as resolved.
Show resolved Hide resolved

/**
* @return whether [this] string represents a Java keyword
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin",

companion object {
const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml"
private const val TEST_TIMEOUT_SECONDS = 20L
private const val TEST_TIMEOUT_SECONDS = 25L
val unfixedLintErrors: MutableList<LintError> = mutableListOf()

// by default using same yml config as for diktat code style check, but allow to override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,25 @@ foo()
} ?: boo()
}

fun nullCheckWithAssumption() {
val a: Int? = 5
a?.let {
foo()
} ?: run {
a = 5
}
a?.let {
foo()
} ?: run {
a = 5
}
a?.let {
a = 5
} ?: foo()
a?.let {
a = 5
} ?: foo()
a?.let {
foo()
} ?: a == 5
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,31 @@ fun reversedCheckSmartCases() {
}
}

fun nullCheckWithAssumption() {
val a: Int? = 5
if (a != null) {
foo()
} else {
a = 5
}
if (a == null) {
a = 5
} else {
foo()
}
if (a != null) {
a = 5
} else {
foo()
}
if (a == null) {
foo()
} else {
a = 5
}
if (a != null) {
foo()
} else {
a == 5
}
}