diff --git a/CHANGELOG.md b/CHANGELOG.md index d56de5ad02..06e965ecc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added ### Fixed - Fix false positives for SpacingBetweenDeclarationsWithAnnotationsRule ([#1125](https://github.com/pinterest/ktlint/issues/1125)) +- Fix false positive with eol comment (`annotation-spacing`) ([#1124](https://github.com/pinterest/ktlint/issues/1124)) ### Changed - Updated to dokka 1.4.32 ([#1148](https://github.com/pinterest/ktlint/pull/1148)) ### Removed diff --git a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationSpacingRule.kt b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationSpacingRule.kt index 41f061f12e..860fa8b468 100644 --- a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationSpacingRule.kt +++ b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationSpacingRule.kt @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl import org.jetbrains.kotlin.psi.KtAnnotationEntry import org.jetbrains.kotlin.psi.psiUtil.children import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.psi.psiUtil.leaves /** * Ensures annotations occur immediately prior to the annotated construct @@ -74,6 +75,10 @@ class AnnotationSpacingRule : Rule("annotation-spacing") { } ) if (next != null) { + if (next.isPartOf(ElementType.EOL_COMMENT) && + next.leaves(forward = false).takeWhile { it.isWhiteSpace() }.none { "\n" in it.text } + ) return + if (node.elementType != ElementType.FILE_ANNOTATION_LIST) { val psi = node.psi emit(psi.endOffset - 1, ERROR_MESSAGE, true) diff --git a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationSpacingRuleTest.kt b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationSpacingRuleTest.kt index 82e438d006..ac25a4a31d 100644 --- a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationSpacingRuleTest.kt +++ b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/AnnotationSpacingRuleTest.kt @@ -425,4 +425,17 @@ class AnnotationSpacingRuleTest { """.trimIndent() ) } + + @Test + fun `lint eol comment on the same line as the annotation`() { + assertThat( + AnnotationSpacingRule().lint( + """ + @SuppressWarnings // foo + fun bar() { + } + """.trimIndent() + ) + ).isEmpty() + } }