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 NewlinesRule.kt #1853

Merged
merged 2 commits into from
Dec 11, 2023
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 @@ -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
Loading