pinterest / ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
https://pinterest.github.io/ktlint/
MIT License
6.15k stars 503 forks source link

`function-literal` rule conflict with maximum line length #2743

Closed mekutluca closed 1 week ago

mekutluca commented 1 month ago

Every time the code is formatted with ktlint, the code block below alternates between the two states given at the Observed Behavior.

Expected Behavior

I expect this to be accepted without raising an error.

                                          X
someLongNamedVariable.someField?.let { 
        field 
    ->

Observed Behavior

Below code gives us the error: Newline expected before parameter (standard:function-literal)

                                          X
someLongNamedVariable.someField?.let { field ->

However, below code gives us the error: No newline expected before parameter (standard:function-literal)

                                          X
someLongNamedVariable.someField?.let { 
        field 
    ->

Steps to Reproduce

Example code provided above.

Your Environment

paul-dingemans commented 1 month ago

The max_line_length setting is invalid. Use either off or unset as value to disable the max line length.

paul-dingemans commented 2 weeks ago

The max_line_length setting is invalid. Use either off or unset as value to disable the max line length.

In hindsight the "solution" above is really crude. What I meant to say was that if you really want to disable the max-line-length checking, that you need to set below:

max_line_length = unset

Disabling the max-line-length rule with ktlint_standard_max-line-length = disabled only disables that specific rule. But any other rule that uses the property max_line_length will not be affected by that. In this case the function-literal rule is using the max_line_length to decide whether it should report a violation regarding the positioning of the variables of the lambda.

The "mistake" you made is very understandable and you can not be blamed for it. Even in case it would have been documented properly (which it isn't), this misleads others as well. Ktlint should be more fault tolerant.