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

False positive for 'Missing newline after "->" (indent)' #1329

Closed
lns-ross opened this issue Dec 29, 2021 · 6 comments · Fixed by #1403
Closed

False positive for 'Missing newline after "->" (indent)' #1329

lns-ross opened this issue Dec 29, 2021 · 6 comments · Fixed by #1403

Comments

@lns-ross
Copy link

Observed Behavior

Linting the code below reports:

no_newline.kt:14:19: Missing newline after "->" (indent)

Steps to Reproduce

Use standard ktlint -v command on this code:

fun demo() {
    val list = listOf(1, 2, 3)

    // These are fine
    list.map { x ->
        /* A comment */
        println("Item: $x")
    }
    list.map { x -> /* A comment */
        println("Item: $x")
    }

    // This one shows the problem
    list.map { x ->
        /* A comment */ println("Item: $x")
    }
}

Your Environment

  • Version of ktlint used: 0.43.2
  • Kotlin: 1.6.10
@paul-dingemans
Copy link
Collaborator

Interesting example. The bug is reported at line 19 while the example only contains 18 line (including the blank after the closing brace). Without the comment on line 15, the error disappears.

@lns-ross
Copy link
Author

no_newline.kt:14:19: Missing newline after "->" (indent)

Easy to mix up ... line 14, column 19
which is the '>'

@paul-dingemans
Copy link
Collaborator

no_newline.kt:14:19: Missing newline after "->" (indent)

Easy to mix up ... line 14, column 19 which is the '>'

Oops, missed that.

The interesting part of the example was however that the block comment lead to this result. Personally, I do think that using a block comment in this way should be avoided and that below should be preferred:

    list.map { x ->
        /* A comment of
         * multiple lines
         */
        println("Item: $x")
    }
    list.map { x ->
        // end of line comment
        println("Item: $x")
    }

I expect that block comments also will break other wrapping rules for the same reason. So I am wondering whether it might be better to have a new comment rule that forces block comments and KDocs to start and end on a line that does not contain any other code element.

@paul-dingemans
Copy link
Collaborator

So I am wondering whether it might be better to have a new comment rule that forces block comments and KDocs to start and end on a line that does not contain any other code element.

New experimental rules comment-wrapping and kdoc-wrapping are added in #1403. These will prevent the problem as construct below is not allowed anymore:

/* A comment */ println("Item: $x")

paul-dingemans added a commit that referenced this issue Mar 13, 2022
Add new experimental rules for wrapping of block comments and KDoc comments

Closes #1329
@lns-ross
Copy link
Author

My one observation is that a single-line block comment is the ONLY way to add a comment at the beginning of the line. Not common but sometimes necessary/preferred. We don't use it often (usually only to tag a line as inserted/modified for debugging). In fact, the bug made the missed ones easier to find afterwards. :-)

Does disabling the new rule(s) (selectively or otherwise) also remove the false-positive in the indent rule? I didn't see an obvious resolution to that in the code of the PR. May be it was included as a fix in another PR?

Thanks for resolving this.

@paul-dingemans
Copy link
Collaborator

paul-dingemans commented Mar 15, 2022

My one observation is that a single-line block comment is the ONLY way to add a comment at the beginning of the line.

True

Not common but sometimes necessary/preferred. We don't use it often (usually only to tag a line as inserted/modified for debugging). In fact, the bug made the missed ones easier to find afterwards. :-)

We seem to disagree on the necessary/preferred part ;-)

Does disabling the new rule(s) (selectively or otherwise) also remove the false-positive in the indent rule? I didn't see an obvious resolution to that in the code of the PR. May be it was included as a fix in another PR?

No, the error will still be there when the new rules are disabled. Solving this error would add unwanted complexity to the Indent Rule whcih already is bloated with lots of functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants