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

[standard:function-literal] Lambda arrow is removed too aggressively, produces non-compiling code #2465

Closed
mateuszkwiecinski opened this issue Dec 26, 2023 · 1 comment · Fixed by #2469
Milestone

Comments

@mateuszkwiecinski
Copy link
Contributor

Probably related to #2331

Expected Behavior

I expect the code compile after ktlint reformats the file

Observed Behavior

Formatted file contains code that doesn't compile

Steps to Reproduce

  1. Run format on the following code:
fun repro(foo: Foo) {
    foo.onClick = if (Math.random() > 0.5) { -> } else null
}

class Foo(var onClick: (() -> Unit)?)
  1. The lambda is removed
image
  1. Compilation fails with Type mismatch: inferred type is Unit but (() -> Unit)? was expected

The alternative syntax would be:

foo.onClick = if (Math.random() > 0.5) {
    { }
} else {
    null
}

but it doesn't fit in a single line, so it may not be preferred in some cases

Your Environment

  • Version of ktlint used: 1.1.0
  • Relevant parts of the .editorconfig settings
ktlint_code_style = intellij_idea
ktlint_experimental = enabled

(I believe this should be enough)

  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task):
  • Version of Gradle used (if applicable):
  • Operating System and version:
@paul-dingemans
Copy link
Collaborator

Tnx for reporting. the one liner seems to have more problems from Ktlint's perspective, but autocorrect should never resulted in compilation errors.

@paul-dingemans paul-dingemans added this to the 1.1.1 milestone Dec 26, 2023
paul-dingemans added a commit that referenced this issue Dec 27, 2023
… empty

Removal of the arrow in this case would lead to a compilation error as the function literal is changed to a normal block when removing the arrow.

Closes #2465
paul-dingemans added a commit that referenced this issue Dec 29, 2023
… empty (#2469)

Removal of the arrow in this case would lead to a compilation error as the function literal is changed to a normal block when removing the arrow.

Closes #2465
paul-dingemans added a commit to Goooler/ktlint that referenced this issue Jan 2, 2024
… empty (pinterest#2469)

Removal of the arrow in this case would lead to a compilation error as the function literal is changed to a normal block when removing the arrow.

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

Successfully merging a pull request may close this issue.

2 participants