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

--format of experimental:function-signature violates max-line-length #1527

Closed
pkubowicz opened this issue Jun 27, 2022 · 3 comments · Fixed by #1566
Closed

--format of experimental:function-signature violates max-line-length #1527

pkubowicz opened this issue Jun 27, 2022 · 3 comments · Fixed by #1566
Milestone

Comments

@pkubowicz
Copy link

Expected Behavior

  • calling --format with experimental:function-signature enabled does not change the code at all if whole function definition does not fit into line length
  • experimental:function-signature does not report an error if a function expression is in multiple lines that cannot be safely joined
  • generally, calling --format of experimental:function-signature should not introduce new rule violations as it happens in the example below

Observed Behavior

gradle lintKotlin

> Task :lintKotlinMain FAILED
src/main/kotlin/com/example/FunctionSignature.kt:4:91: Lint error > [experimental:function-signature] First line of body expression fits on same line as function signature

gradle formatKotlin

> Task :formatKotlinMain
src/main/kotlin/com/example/FunctionSignature.kt:4:1: Format could not fix > [max-line-length] Exceeded max line length (100)
src/main/kotlin/com/example/FunctionSignature.kt:5:1: Format could not fix > [experimental:function-signature] Newline expected before expression body
src/main/kotlin/com/example/FunctionSignature.kt: Format fixed

Steps to Reproduce

Original file:

package com.example

class FunctionSignature {
    fun isSplitIntoMultipleLinesToFitIntoLineLengthLimit(param1: String, param2: String) =
        param1 + param2 + param1
}

.editorconfig:

[*.{kt,kts}]
max_line_length=100

Run ktlint --format.

File produced by ktlint (incorrect):

package com.example

class FunctionSignature {
    fun isSplitIntoMultipleLinesToFitIntoLineLengthLimit(param1: String, param2: String) = param1 + param2 + param1
}

Your Environment

@paul-dingemans
Copy link
Collaborator

Output below is not produced by ktlint but the kotlinter gradle plugin that you are using.

gradle lintKotlin

> Task :lintKotlinMain FAILED
src/main/kotlin/com/example/FunctionSignature.kt:4:91: Lint error > [experimental:function-signature] First line of body expression fits on same line as function signature

gradle formatKotlin

> Task :formatKotlinMain
src/main/kotlin/com/example/FunctionSignature.kt:4:1: Format could not fix > [max-line-length] Exceeded max line length (100)
src/main/kotlin/com/example/FunctionSignature.kt:5:1: Format could not fix > [experimental:function-signature] Newline expected before expression body
src/main/kotlin/com/example/FunctionSignature.kt: Format fixed

When running with the CLI you have to invoke command to enable the experimental rule:

ktlint --experimental -F

This results in output:

package com.example

class FunctionSignature {
    fun isSplitIntoMultipleLinesToFitIntoLineLengthLimit(param1: String, param2: String) =
        param1 + param2 + param1
}

Please check the configuration of the kotlinter plugin that it runs the experimental rules as well.

@pkubowicz
Copy link
Author

Output below is not produced by ktlint

The point of this bug is not that the output looks ugly, the point is what ktlint auto-correction function does with Kotlin sources. This does not depend on the way you call ktlint.

you have to invoke command to enable the experimental rule

I don't want to invoke ktlint from the command line, or download it manually. Are there any people except for its maintainers who do such things?

But sure, let's waste some more time. Here is the output from my console.

pkubowicz:~/devel/bugs/ktlint-paren(main)% ./ktlint-0-46-1          
pkubowicz:~/devel/bugs/ktlint-paren(main)% ./ktlint-0-46-1 --experimental
/home/pkubowicz/devel/bugs/ktlint-paren/src/main/kotlin/com/example/FunctionSignature.kt:4:91: First line of body expression fits on same line as function signature (experimental:function-signature)
pkubowicz:~/devel/bugs/ktlint-paren(main)% ./ktlint-0-46-1 --experimental -F
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.jetbrains.kotlin.com.intellij.util.ReflectionUtil (file:/home/pkubowicz/devel/bugs/ktlint-paren/ktlint-0-46-1) to field java.lang.Throwable.backtrace
WARNING: Please consider reporting this to the maintainers of org.jetbrains.kotlin.com.intellij.util.ReflectionUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
/home/pkubowicz/devel/bugs/ktlint-paren/src/main/kotlin/com/example/FunctionSignature.kt:4:1: Exceeded max line length (100) (cannot be auto-corrected) (max-line-length)
/home/pkubowicz/devel/bugs/ktlint-paren/src/main/kotlin/com/example/FunctionSignature.kt:5:1: Newline expected before expression body (experimental:function-signature)
pkubowicz:~/devel/bugs/ktlint-paren(main)U% git di
diff --git a/src/main/kotlin/com/example/FunctionSignature.kt b/src/main/kotlin/com/example/FunctionSignature.kt
index ecf379f..4a695d5 100644
--- a/src/main/kotlin/com/example/FunctionSignature.kt
+++ b/src/main/kotlin/com/example/FunctionSignature.kt
@@ -1,6 +1,5 @@
 package com.example
 
 class FunctionSignature {
-    fun isSplitIntoMultipleLinesToFitIntoLineLengthLimit(param1: String, param2: String) =
-        param1 + param2 + param1
+    fun isSplitIntoMultipleLinesToFitIntoLineLengthLimit(param1: String, param2: String) = param1 + param2 + param1
 }
pkubowicz:~/devel/bugs/ktlint-paren(main)U% ./ktlint-0-46-1                  
/home/pkubowicz/devel/bugs/ktlint-paren/src/main/kotlin/com/example/FunctionSignature.kt:4:1: Exceeded max line length (100) (cannot be auto-corrected) (max-line-length)
pkubowicz:~/devel/bugs/ktlint-paren(main)U% ./ktlint-0-46-1 --version
0.46.1

@paul-dingemans
Copy link
Collaborator

paul-dingemans commented Jul 1, 2022

Ok, the problem is that with each invocation of ktlint --experimental -F the error alternates between:

First line of body expression fits on same line as function signature (experimental:function-signature)

and

Newline expected before expression body (experimental:function-signature)

I would have expected that neither of them were reported as it is an error which is autocorrected and such errors are not reported when running with -F. Most likely the autocorrected error results in a new violation which is only seen at the next invocation.

For now, you can disable the entire rule or set following in the .editorconfig:

ktlint_function_signature_body_expression_wrapping = always

@paul-dingemans paul-dingemans added this to the 0.47.0 milestone Jul 1, 2022
paul-dingemans added a commit to paul-dingemans/ktlint that referenced this issue Aug 2, 2022
paul-dingemans added a commit that referenced this issue Aug 6, 2022
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