diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRule.kt index f71e015f6f..6ef87e2d83 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRule.kt @@ -15,6 +15,7 @@ class FinalNewlineRule : Rule("final-newline"), Rule.Modifier.RestrictToRoot { emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit ) { if (node.isRoot()) { + if (node.textLength == 0) return val editorConfig = node.getUserData(KtLint.EDITOR_CONFIG_USER_DATA_KEY)!! val insertFinalNewline = editorConfig.insertFinalNewline val lastNode = lastChildNodeOf(node) diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRuleTest.kt index a39735cbb8..619e61d244 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRuleTest.kt @@ -23,11 +23,7 @@ class FinalNewlineRuleTest { "", mapOf("insert_final_newline" to "true") ) - ).isEqualTo( - listOf( - LintError(1, 1, "final-newline", "File must end with a newline (\\n)") - ) - ) + ).isEmpty() assertThat( FinalNewlineRule().lint( "fun name() {\n}", @@ -52,6 +48,12 @@ class FinalNewlineRuleTest { ) ).isEmpty() // false + assertThat( + FinalNewlineRule().lint( + "", + mapOf("insert_final_newline" to "false") + ) + ).isEmpty() assertThat( FinalNewlineRule().lint( "fun name() {\n}",