From 5d47cb558955a0e6a38cdae94ab7607683336fd5 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Mon, 26 Apr 2021 13:40:43 +0300 Subject: [PATCH 1/5] bugfix/useless-autofix-in-null-checks(#840) ### What's done: * Fixed bug --- .../cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt | 2 +- .../paragraph4/null_checks/IfConditionNullCheckExpected.kt | 4 ++++ .../test/paragraph4/null_checks/IfConditionNullCheckTest.kt | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt index 1d897a65f5..2b81a0a863 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt @@ -104,7 +104,7 @@ class NullChecksRule(configRules: List) : DiktatRule( """.trimMargin() } } else { - if (elseCodeLines.isNullOrEmpty()) { + if (elseCodeLines.isNullOrEmpty() || elseCodeLines.first() == "null") { "$variableName?.let {\n${thenCodeLines?.joinToString(separator = "\n")}\n}" } else { """ diff --git a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt index e994196c76..65e1c7d59a 100644 --- a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt @@ -28,6 +28,10 @@ print("qqq") } ?: run { print("www") +} + + some?.let { +print("ttt") } } diff --git a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt index 5e7d750744..a66e74862e 100644 --- a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt @@ -27,5 +27,11 @@ fun test() { } else { print("www") } + + if (some != null) { + print("ttt") + } else { + null + } } From dba521dc740636c9cb47ca636c5fbb0f04ea5153 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Mon, 26 Apr 2021 14:19:00 +0300 Subject: [PATCH 2/5] bugfix/useless-autofix-in-null-checks(#840) ### What's done: * Fixed bug --- .../cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt | 2 +- .../null_checks/IfConditionNullCheckExpected.kt | 8 ++++++++ .../paragraph4/null_checks/IfConditionNullCheckTest.kt | 7 +++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt index 2b81a0a863..215cad8cb2 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt @@ -104,7 +104,7 @@ class NullChecksRule(configRules: List) : DiktatRule( """.trimMargin() } } else { - if (elseCodeLines.isNullOrEmpty() || elseCodeLines.first() == "null") { + if (elseCodeLines.isNullOrEmpty() || (elseCodeLines.first() == "null" && elseCodeLines.size == 1)) { "$variableName?.let {\n${thenCodeLines?.joinToString(separator = "\n")}\n}" } else { """ diff --git a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt index 65e1c7d59a..39f5f9b947 100644 --- a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt @@ -32,6 +32,14 @@ print("www") some?.let { print("ttt") +} + + some?.let { +print("ttt") +} +?: run { +null +value } } diff --git a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt index a66e74862e..6f05556304 100644 --- a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt @@ -33,5 +33,12 @@ fun test() { } else { null } + + if (some != null) { + print("ttt") + } else { + null + value + } } From 86114152dcc91d7b97bddc381971958387bee0a1 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Mon, 26 Apr 2021 15:26:15 +0300 Subject: [PATCH 3/5] bugfix/useless-autofix-in-null-checks(#840) ### What's done: * Fixed bug --- .../org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt index 215cad8cb2..02f7dd1572 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt @@ -104,7 +104,7 @@ class NullChecksRule(configRules: List) : DiktatRule( """.trimMargin() } } else { - if (elseCodeLines.isNullOrEmpty() || (elseCodeLines.first() == "null" && elseCodeLines.size == 1)) { + if (elseCodeLines.isNullOrEmpty() || (elseCodeLines.singleOrNull() == "null")) { "$variableName?.let {\n${thenCodeLines?.joinToString(separator = "\n")}\n}" } else { """ From 42c73c0a4f8a944d43b7a814b0e3c1da4cf9e571 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Mon, 26 Apr 2021 15:59:52 +0300 Subject: [PATCH 4/5] bugfix/useless-autofix-in-null-checks(#840) ### What's done: * Fixed bug --- .../ruleset/rules/chapter4/NullChecksRule.kt | 32 ++++++++++++------- .../IfConditionNullCheckExpected.kt | 4 +++ .../null_checks/IfConditionNullCheckTest.kt | 6 ++++ 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt index 02f7dd1572..d05366a753 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt @@ -91,17 +91,27 @@ class NullChecksRule(configRules: List) : DiktatRule( val thenCodeLines = condition.extractLinesFromBlock(THEN) val elseCodeLines = condition.extractLinesFromBlock(ELSE) val text = if (isEqualToNull) { - if (elseCodeLines.isNullOrEmpty()) { - "$variableName ?: run {\n${thenCodeLines?.joinToString(separator = "\n")}\n}" - } else { - """ - |$variableName?.let { - |${elseCodeLines.joinToString(separator = "\n")} - |} - |?: run { - |${thenCodeLines?.joinToString(separator = "\n")} - |} - """.trimMargin() + when { + elseCodeLines.isNullOrEmpty() -> { + "$variableName ?: run {\n${thenCodeLines?.joinToString(separator = "\n")}\n}" + } + thenCodeLines!!.singleOrNull() == "null" -> { + """ + |$variableName?.let { + |${elseCodeLines.joinToString(separator = "\n")} + |} + """.trimMargin() + } + else -> { + """ + |$variableName?.let { + |${elseCodeLines.joinToString(separator = "\n")} + |} + |?: run { + |${thenCodeLines?.joinToString(separator = "\n")} + |} + """.trimMargin() + } } } else { if (elseCodeLines.isNullOrEmpty() || (elseCodeLines.singleOrNull() == "null")) { diff --git a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt index 39f5f9b947..2a05252416 100644 --- a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckExpected.kt @@ -21,6 +21,10 @@ print("qwe") } ?: run { print("asd") +} + + some?.let { +print("qweqwe") } some?.let { diff --git a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt index 6f05556304..5936fcb536 100644 --- a/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph4/null_checks/IfConditionNullCheckTest.kt @@ -22,6 +22,12 @@ fun test() { print("qwe") } + if (some == null) { + null + } else { + print("qweqwe") + } + if (some != null) { print("qqq") } else { From a31ea30ac214088ef059317d819a626b2fe9c1d3 Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Tue, 27 Apr 2021 12:32:09 +0300 Subject: [PATCH 5/5] bugfix/useless-autofix-in-null-checks(#840) ### What's done: * code style --- .../ruleset/rules/chapter4/NullChecksRule.kt | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt index d05366a753..1db26b8083 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter4/NullChecksRule.kt @@ -83,7 +83,7 @@ class NullChecksRule(configRules: List) : DiktatRule( } } - @Suppress("UnsafeCallOnNullableType") + @Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION") private fun fixNullInIfCondition(condition: ASTNode, binaryExpression: KtBinaryExpression, isEqualToNull: Boolean) { @@ -92,26 +92,20 @@ class NullChecksRule(configRules: List) : DiktatRule( val elseCodeLines = condition.extractLinesFromBlock(ELSE) val text = if (isEqualToNull) { when { - elseCodeLines.isNullOrEmpty() -> { - "$variableName ?: run {\n${thenCodeLines?.joinToString(separator = "\n")}\n}" - } - thenCodeLines!!.singleOrNull() == "null" -> { - """ + elseCodeLines.isNullOrEmpty() -> "$variableName ?: run {\n${thenCodeLines?.joinToString(separator = "\n")}\n}" + thenCodeLines!!.singleOrNull() == "null" -> """ |$variableName?.let { |${elseCodeLines.joinToString(separator = "\n")} |} """.trimMargin() - } - else -> { - """ + else -> """ |$variableName?.let { |${elseCodeLines.joinToString(separator = "\n")} |} |?: run { - |${thenCodeLines?.joinToString(separator = "\n")} + |${thenCodeLines.joinToString(separator = "\n")} |} """.trimMargin() - } } } else { if (elseCodeLines.isNullOrEmpty() || (elseCodeLines.singleOrNull() == "null")) {