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

NPE in COMPLEX_BOOLEAN_EXPRESSION rule #1052

Merged
merged 2 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -115,7 +115,7 @@ enum class Warnings(
WRONG_NEWLINES(true, "3.6.2", "incorrect line breaking"),
TRAILING_COMMA(true, "3.6.2", "use trailing comma"),
COMPLEX_EXPRESSION(false, "3.6.3", "complex dot qualified expression should be replaced with variable"),
COMPLEX_BOOLEAN_EXPRESSION(true, "3.6.4", "too complex boolean expression, that can be simplified"),
COMPLEX_BOOLEAN_EXPRESSION(true, "3.6.4", "simplification could be produced for the too complex boolean expression"),

// FixMe: autofixing will be added for this rule
STRING_CONCATENATION(true, "3.15.1", "strings should not be concatenated using plus operator - use string templates instead if the statement fits one line"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,13 @@ class BooleanExpressionsRule(configRules: List<RulesConfig>) : DiktatRule(
.toString()
.replace("&", "&&")
.replace("|", "||")
.drop(1) // dropping first (
.dropLast(1) // dropping last )

if (simplifiedExpr.toString().first() == '(' && simplifiedExpr.toString().last() == ')') {
correctKotlinBooleanExpression = correctKotlinBooleanExpression
.drop(1)
.dropLast(1)
}

mapOfExpressionToChar.forEach { (key, value) ->
correctKotlinBooleanExpression = correctKotlinBooleanExpression.replace(value.toString(), key)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ class BooleanExpressionsRuleFixTest : FixTestBase("test/paragraph3/boolean_expre
fun `check distributive law fixing`() {
fixAndCompare("DistributiveLawExpected.kt", "DistributiveLawTest.kt")
}

@Test
@Tag(WarningNames.COMPLEX_BOOLEAN_EXPRESSION)
fun `check same expressions`() {
fixAndCompare("SameExpressionsInConditionExpected.kt", "SameExpressionsInConditionTest.kt")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fun foo() {
if (a) {}
if (a) {}
if (a) {}

return if (node is TomlKeyValueSimple) {
decodeValue().toString().toLowerCase() != "null"
} else {
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fun foo() {
if (a || a) {}
if (a && a) {}
if ((((a && a)))) {}

return if ((node is TomlKeyValueSimple) || (node is TomlKeyValueSimple)) {
decodeValue().toString().toLowerCase() != "null"
} else {
true
}
}