pinterest / ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
https://pinterest.github.io/ktlint/
MIT License
6.06k stars 504 forks source link

Binary operators in `!(...)` not checked for missing horizontal whitespace #2652

Closed cflee closed 1 month ago

cflee commented 1 month ago

Expected Behavior

Given this code:

val foo1 = !(null?:true)
val foo2 = !(7==7)

It should be a lint violation, and formatted to the following to match what IntelliJ does:

val foo1 = !(null ?: true)
val foo2 = !(7 == 7)

Kotlin Coding Conventions: Horizontal whitespace

Put spaces around binary operators (a + b). Exception: don't put spaces around the "range to" operator (0..i).

This rule currently seems to be implemented in SpacingAroundOperatorsRule.

Observed Behavior

No error is thrown during check and it is not formatted.

However, it is working correctly for these cases:

val foo1 = (null?:true)
val foo1 = null?:true

So I am only observing it for the !( ... ) cases.

Steps to Reproduce

Adding this test to SpacingAroundOperatorsRuleTest:

@Test
fun `Given operator inside !() missing spaces around the operator`() {
    val code =
        """
    val foo1 = !(null?:true)
    val foo2 = !(7==7)
    val foo3 = !(null?: true)
    val foo4 = (null?:true)
    val foo5 = null?:true
    """.trimIndent()
    val formattedCode =
        """
    val foo1 = !(null ?: true)
    val foo2 = !(7 == 7)
    val foo3 = !(null ?: true)
    val foo4 = (null ?: true)
    val foo5 = null ?: true
    """.trimIndent()
    spacingAroundOperatorsRuleAssertThat(code)
        .hasLintViolations(
            LintViolation(1, 18, "Missing spacing around \"?:\""),
            LintViolation(2, 15, "Missing spacing around \"==\""),
            LintViolation(3, 18, "Missing spacing before \"?:\""),
            LintViolation(4, 17, "Missing spacing around \"?:\""),
            LintViolation(5, 16, "Missing spacing around \"?:\""),
        ).isFormattedAs(formattedCode)
}

Results in this test failure:

[Lint errors which can be automatically corrected] 
Expecting actual:
  [LintViolationFields(line=4, col=17, detail=Missing spacing around "?:", canBeAutoCorrected=true),
    LintViolationFields(line=5, col=16, detail=Missing spacing around "?:", canBeAutoCorrected=true)]
to contain exactly in any order:
  [LintViolationFields(line=1, col=18, detail=Missing spacing around "?:", canBeAutoCorrected=true),
    LintViolationFields(line=2, col=15, detail=Missing spacing around "==", canBeAutoCorrected=true),
    LintViolationFields(line=3, col=18, detail=Missing spacing before "?:", canBeAutoCorrected=true),
    LintViolationFields(line=4, col=17, detail=Missing spacing around "?:", canBeAutoCorrected=true),
    LintViolationFields(line=5, col=16, detail=Missing spacing around "?:", canBeAutoCorrected=true)]
but could not find the following elements:
  [LintViolationFields(line=1, col=18, detail=Missing spacing around "?:", canBeAutoCorrected=true),
    LintViolationFields(line=2, col=15, detail=Missing spacing around "==", canBeAutoCorrected=true),
    LintViolationFields(line=3, col=18, detail=Missing spacing before "?:", canBeAutoCorrected=true)]
...

Your Environment