saveourtool/diktat

`WRONG_INDENTATION` rule behaving incorrectly with superclass constructor call arguments

Open

#1404 aperta il 27 giu 2022

Vedi su GitHub
 (3 commenti) (0 reazioni) (1 assegnatario)Kotlin (40 fork)github user discovery
buggood first issueindentation

Metriche repository

Star
 (571 star)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

Both this

class Klass : Base("foo")

and this superclass constructor calls are indented correctly, in full accordance with 3.3. Indentation:

class Klass :
    Base("foo")

Now consider we want to split the superclass constructor call arguments into multiple lines:

class Klass : Base(
    "foo"
)

Even this is correct, too. Now, let's put a newline before the superclass call:

class Klass :
    Base(
        "foo"
    )

DiKTat will immediately complain:

Klass.kt:3:1: [WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 4 but was 8 (diktat-ruleset:indentation)
Klass.kt:4:1: [WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 0 but was 4 (diktat-ruleset:indentation)

So apparently DiKTat expects the following formatting (outright wrong):

class Klass :
    Base(
    "foo"
)

Steps to Reproduce

As it's obvious from the above example, two conditions must be met for the issue to manifest itself:

  1. The supertype constructor call should be put on a separate line.
  2. The supertype constructor call arguments should be put on individual lines.

There're more real-life examples which trigger this issue, e. g.:

class Klass :
    Base(
        "foo",
        ::Klass,
        listOf(
            "foo",
            "bar",
            "baz"
        )
    ),
    Cloneable,
    CharSequence {
    // ...
}

or even

class IndentationRuleFixTest :
    FixTestBase(
        "test/paragraph3/indentation",
        ::IndentationRule,
        listOf(
            RulesConfig(WRONG_INDENTATION.name, true,
                mapOf(
                    "newlineAtEnd" to "true",
                    "extendedIndentOfParameters" to "true",
                    "alignedParameters" to "true",
                    "extendedIndentAfterOperators" to "true",
                    "extendedIndentBeforeDot" to "true",
                )
            )
        )
    ),
    IndentationRuleTestMixin,
    IndentationRuleTestResources {
    // ...
}

Environment information

  • diktat version: 1.2.0
  • build tool (maven/gradle): Maven
  • how is diktat run (CLI, plugin, etc.): Maven plug-in
  • kotlin version: 1.7.0
  • operating system: Windows

Guida contributor