Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WRONG_INDENTATION continuation indent expected in if-expression #1351

Open
icemachined opened this issue Jun 7, 2022 · 2 comments · Fixed by #1433
Open

WRONG_INDENTATION continuation indent expected in if-expression #1351

icemachined opened this issue Jun 7, 2022 · 2 comments · Fixed by #1433
Assignees
Labels
bug Something isn't working indentation Problems with `IndentationRule` (`WRONG_INDENTATION`)

Comments

@icemachined
Copy link
Member

icemachined commented Jun 7, 2022

Describe the bug

Expected behavior

there should be no error after idea style applying

Observed behavior

  "level": "error",
          "locations": [
            {
              "physicalLocation": {
                "artifactLocation": {
                  "uri": "diktat-rules\\src\\main\\kotlin\\org\\cqfn\\diktat\\ruleset\\rules\\chapter2\\kdoc\\KdocComments.kt",
                  "uriBaseId": "%SRCROOT%"
                },
                "region": {
                  "startColumn": 1,
                  "startLine": 116
                }
              }
            }
          ],
          "message": {
            "text": "[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 12 but was 16"
          },
          "ruleId": "diktat-ruleset:zct-indentation"

Steps to Reproduce

  1. If you apply idea formatting in KdocComments you have the following piece of code
        if (valueParameterNode.parents().none { it.elementType == PRIMARY_CONSTRUCTOR } ||
            !valueParameterNode.hasChildOfType(VAL_KEYWORD) && 
            !valueParameterNode.hasChildOfType(VAR_KEYWORD)
        ) {
            return
        }

but diktat report error as far as it expect continuation indent in the last line of if expression like this

        if (valueParameterNode.parents().none { it.elementType == PRIMARY_CONSTRUCTOR } ||
            !valueParameterNode.hasChildOfType(VAL_KEYWORD) && 
                !valueParameterNode.hasChildOfType(VAR_KEYWORD)
        ) {
            return
        }
  1. If you apply idea formatting in KdocComments you have the following piece of code with 16 spaces in second line
        val prevComment = if (valueParameterNode.siblings(forward = false)
                .takeWhile { it.elementType != EOL_COMMENT && it.elementType != BLOCK_COMMENT }
                .all { it.elementType == WHITE_SPACE }
        ) {

but diktat report error as far as it expect 12 spaces instead 16 as below

        val prevComment = if (valueParameterNode.siblings(forward = false)
              .takeWhile { it.elementType != EOL_COMMENT && it.elementType != BLOCK_COMMENT }
              .all { it.elementType == WHITE_SPACE }
        ) {

Environment information

  • diktat version: 1.1.1-SNAPSHOT
  • build tool (maven/gradle):
  • how is diktat run (CLI, plugin, etc.):
  • kotlin version:
  • operating system:
  • link to a project (if your project is public):
@icemachined icemachined added the bug Something isn't working label Jun 7, 2022
@0x6675636b796f75676974687562 0x6675636b796f75676974687562 changed the title [WRONG_INDENTATION] continuation indent expected in if expression [WRONG_INDENTATION] continuation indent expected in if expression Jun 14, 2022
@0x6675636b796f75676974687562 0x6675636b796f75676974687562 added this to the 1.2.0 milestone Jun 14, 2022
@0x6675636b796f75676974687562 0x6675636b796f75676974687562 changed the title [WRONG_INDENTATION] continuation indent expected in if expression [WRONG_INDENTATION] continuation indent expected in if-expression Jul 6, 2022
@0x6675636b796f75676974687562
Copy link
Member

0x6675636b796f75676974687562 commented Jul 7, 2022

Reg. No. 2, from IDEA standpoint, you get a relative indent of 8 (an absolute indent of 16), because IDEA combines a single indent within an if()-statement (CONTINUATION_INDENT_IN_IF_CONDITIONS is false) with a single indent of a chained function call (CONTINUATION_INDENT_FOR_CHAINED_CALLS is false):

val prevComment = if (valueParameterNode.siblings(forward = false)
        .takeWhile { it.elementType != EOL_COMMENT && it.elementType != BLOCK_COMMENT }
        .all { it.elementType == WHITE_SPACE }
) {
    // block body
}

Minimal repro:

if (""
        .isBlank()) {
    // block body
}
while (""
        .isBlank()) {
    // block body
}
do {
    // block body
} while (""
        .isBlank())

This behaviour is documented in this Wiki section.

@0x6675636b796f75676974687562
Copy link
Member

0x6675636b796f75676974687562 commented Jul 7, 2022

No. 1 is a separate matter. While all these are correctly formatted (IDEA and diKTat behave consistently):

if (true &&
    true &&
    false) {
    return
}

if (true ||
    true ||
    false) {
    return
}

if (true &&
    true ||
    false) {
    return
}

— here's the minimal repro:

if (true ||
    true &&
        false) {
    return
}

Apparently, this is something related to operator priorities (see 3.5 Line length), since adding parentheses around the 1st two booleans immediately "fixes" the issue (and modifies the logic):

if ((true ||
    true) &&
    false) {
    return
}

Another minimal repro:

if (1 +
    2 *
        3 == 7) {
    return
}

And two more, w/o any if()-statements:

val a = true ||
    true &&
        true

val b = 1 +
    2 *
        3

In this latter case, IDEA would disregard any operator priorities and simply use a (continuation) indent of 8.

0x6675636b796f75676974687562 added a commit that referenced this issue Jul 8, 2022
…ates

### What's done:

 * Unit tests added which cover indentation in `if`- and `while`-expression
   predicates.
 * See #1351.
0x6675636b796f75676974687562 added a commit that referenced this issue Jul 11, 2022
…ates (#1433)

* Add unit tests for indentation in `if`- and `while`-expression predicates

### What's done:

 * Unit tests added which cover indentation in `if`- and `while`-expression
   predicates.
 * See #1351.
@0x6675636b796f75676974687562 0x6675636b796f75676974687562 removed this from the 1.2.4 milestone Aug 10, 2022
@0x6675636b796f75676974687562 0x6675636b796f75676974687562 added this to the 1.2.5 milestone Nov 9, 2022
@0x6675636b796f75676974687562 0x6675636b796f75676974687562 added the indentation Problems with `IndentationRule` (`WRONG_INDENTATION`) label Nov 18, 2022
@0x6675636b796f75676974687562 0x6675636b796f75676974687562 changed the title [WRONG_INDENTATION] continuation indent expected in if-expression WRONG_INDENTATION continuation indent expected in if-expression Nov 18, 2022
@0x6675636b796f75676974687562 0x6675636b796f75676974687562 removed this from the 1.2.5 milestone Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working indentation Problems with `IndentationRule` (`WRONG_INDENTATION`)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants