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

Fix NPE if or operator at start of line followed by dot qualified expression #1994

Merged
merged 1 commit into from
May 5, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
* Print absolute path of file in lint violations when flag "--relative" is not specified in Ktlint CLI ([#1963](https://github.com/pinterest/ktlint/issues/1963))
* Handle parameter `--code-style=android_studio` in Ktlint CLI identical to deprecated parameter `--android` ([#1982](https://github.com/pinterest/ktlint/issues/1982))
* Prevent nullpointer exception (NPE) if class without body is followed by multiple blank lines until end of file `no-consecutive-blank-lines` ([#1987](https://github.com/pinterest/ktlint/issues/1987))
* Prevent nullpointer exception (NPE) if or operator at start of line is followed by dot qualified expression `indent` ([#1993](https://github.com/pinterest/ktlint/issues/1993))

### Changed
* Separated Baseline functionality out of `ktlint-cli` into separate `ktlint-baseline` module for API consumers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ public class IndentationRule :
private fun ASTNode?.isElvisOperator() =
this != null &&
elementType == OPERATION_REFERENCE &&
firstChildNode.elementType == ELVIS
firstChildNode?.elementType == ELVIS

private fun ASTNode.acceptableTrailingSpaces(): String {
require(elementType == WHITE_SPACE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5122,6 +5122,32 @@ internal class IndentationRuleTest {
}
}

@Test
fun `Issue 1993 - An or operator at start of line followed by a dot qualified expressions should not throw an exception`() {
val code =
"""
val foo =
if (false
|| foobar.bar()
) {
// Do something
}
""".trimIndent()
val formattedCode =
"""
val foo =
if (false ||
foobar.bar()
) {
// Do something
}
""".trimIndent()
indentationRuleAssertThat(code)
.addAdditionalRuleProvider { ChainWrappingRule() }
.hasLintViolationForAdditionalRule(3, 9, "Line must not begin with \"||\"")
.isFormattedAs(formattedCode)
}

private companion object {
val INDENT_STYLE_TAB =
INDENT_STYLE_PROPERTY to PropertyType.IndentStyleValue.tab
Expand Down