Skip to content

WRONG_INDENTATION: false positives for parentheses-surrounded infix expressions #1409

@0x6675636b796f75676974687562

Description

The following code fragment triggers no errors, provided that expectedIndentAfterOperators is off:

val value = 1 to
    2

Now, let's surround the expression (which is an infix function call) with parentheses, preserving the indent:

val value = (1 to
    2)

DiKTat will report the following errors:

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 8 but was 4
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 8 but was 4

Strangely, if we move the expression onto the next line (and increment the indent), the errors immediately go away:

val value =
    (1 to
        2)

The issue is caused by the incomplete implementation of ExpressionIndentationChecker as a part of #75 (see 7d913ed).

Note

The fix for this issue will also have the following side-effect. Previously (1.2.0), DiKTat would format expressions wrapped before an infix function (see also #1340) like this:

fun f() {
    true
    || false
}

— unless those expressions are surrounded with parentheses, requiring an extra indent:

fun f() {
    (true
        || false)
}

From now on (1.2.1+), no extra indent will be required:

fun f() {
    (true
    || false)
}

— which is still incorrect, but will be fixed as a part of #1340.

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

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions