Skip to content

Commit

Permalink
NPE in NewlinesRule.kt
Browse files Browse the repository at this point in the history
### What's done: (#1853)
- Fixed NPE on test

Closes #1779
  • Loading branch information
diphtongue authored Dec 11, 2023
1 parent 65d5558 commit 3059a40
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -510,22 +510,21 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
}
}

@Suppress("AVOID_NULL_CHECKS")
private fun handleValueParameterList(node: ASTNode, entryType: String) = node
.children()
.filter {
(it.elementType == COMMA && !it.treeNext.isNewLineNode()) ||
val isNewLineNext = it.treeNext?.isNewLineNode() ?: false
val isNewLinePrev = it.treePrev?.isNewLineNode() ?: false
(it.elementType == COMMA && !isNewLineNext) ||
// Move RPAR to the new line
(it.elementType == RPAR && it.treePrev.elementType != COMMA && !it.treePrev.isNewLineNode())
(it.elementType == RPAR && it.treePrev?.elementType != COMMA && !isNewLinePrev)
}
.toList()
.takeIf { it.isNotEmpty() }
?.let { invalidCommas ->
val warnText = if (node.getParentIdentifier() != null) {
val warnText = node.getParentIdentifier()?.let {
"$entryType should be placed on different lines in declaration of <${node.getParentIdentifier()}>"
} else {
"$entryType should be placed on different lines"
}
} ?: "$entryType should be placed on different lines"
WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode,
warnText, node.startOffset, node) {
invalidCommas.forEach { commaOrRpar ->
Expand All @@ -535,7 +534,7 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
commaOrRpar.appendNewlineMergingWhiteSpace(nextWhiteSpace, nextWhiteSpace.treeNext)
} ?: commaOrRpar.treeNext?.treeParent?.appendNewlineMergingWhiteSpace(nextWhiteSpace, commaOrRpar.treeNext)
} else {
commaOrRpar.treeParent.appendNewlineMergingWhiteSpace(nextWhiteSpace, commaOrRpar)
commaOrRpar.treeParent?.appendNewlineMergingWhiteSpace(nextWhiteSpace, commaOrRpar)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,20 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
)
}

@Test
@Tag(WarningNames.WRONG_NEWLINES)
fun `shouldn't fall with NPE`() {
lintMethod(
"""
|val TEST_FUNC = { _: Int, _: Set<Int>, _: Int, ->
|}
""".trimMargin(),
DiktatError(1, 19, ruleId, "${WRONG_NEWLINES.warnText()} " +
"first parameter should be placed on a separate line or all other parameters should be aligned with it in declaration of <null>", true),
DiktatError(1, 19, ruleId, "${WRONG_NEWLINES.warnText()} value parameters should be placed on different lines", true),
)
}

@Test
@Tag(WarningNames.WRONG_NEWLINES)
fun `not complaining on fun without return type`() {
Expand Down

0 comments on commit 3059a40

Please sign in to comment.