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

Do not merge annotated expression with function signature rule #2043

Closed
paul-dingemans opened this issue May 19, 2023 · 0 comments · Fixed by #2049
Closed

Do not merge annotated expression with function signature rule #2043

paul-dingemans opened this issue May 19, 2023 · 0 comments · Fixed by #2049

Comments

@paul-dingemans
Copy link
Collaborator

Given code below:

private fun getErrorMessage(value: String): String =
        @Suppress("DEPRECATION")
        customMessage.takeUnless { it.isEmpty() } ?: String.format(DEFAULT_ERROR_MESSAGE_WITH_NO_REASON, value)

and assume that the function-signature rule is enabled and the (first line of the) function body expression has to be merged with the function signature.

For example with .editorconfig:

[{*.kt,*.kts}]
ktlint_code_style = android
ktlint_standard=enabled
ktlint_experimental=enabled

the command ktlint --relative results in

17:41:39.417 [main] INFO com.pinterest.ktlint.cli.internal.KtlintCommandLine - Enable default patterns [**/*.kt, **/*.kts]
x.kts:6:97: Unnecessary trailing comma before ")" (standard:trailing-comma-on-call-site)
src/main/kotlin/Foo.kt:1:53: First line of body expression fits on same line as function signature (standard:function-signature)
src/main/kotlin/Foo.kt:3:1: Exceeded max line length (100) (standard:max-line-length)
src/main/kotlin/Foo.kt:3:31: Missing newline after "{" (standard:wrapping)
src/main/kotlin/Foo.kt:3:46: Expected newline (standard:annotation)
src/main/kotlin/Foo.kt:3:64: Argument should be on a separate line (unless all arguments can fit a single line) (standard:argument-list-wrapping)
src/main/kotlin/Foo.kt:3:102: Argument should be on a separate line (unless all arguments can fit a single line) (standard:argument-list-wrapping)
src/main/kotlin/Foo.kt:3:107: Missing newline before ")" (standard:argument-list-wrapping)

Summary error count (descending) by rule:
  standard:argument-list-wrapping: 3
  standard:annotation: 1
  standard:function-signature: 1
  standard:max-line-length: 1
  standard:trailing-comma-on-call-site: 1
  standard:wrapping: 1

When running format the output changes to:

17:44:18.369 [main] INFO com.pinterest.ktlint.cli.internal.KtlintCommandLine - Enable default patterns [**/*.kt, **/*.kts]
src/main/kotlin/Foo.kt:1:53: Expected newline before annotation (standard:annotation)

Summary error count (descending) by rule:
  standard:annotation: 1

This log message is rather confusing when you compare it with the original code sample. However, it should not be compared with the original but with the modified code to make sense.
and the file is formatted as:

private fun getErrorMessage(value: String): String = @Suppress("DEPRECATION")
customMessage.takeUnless { it.isEmpty() }
    ?: String.format(DEFAULT_ERROR_MESSAGE_WITH_NO_REASON, value)

The function-signature should not format in such a way that if violates the annotation rule.

Originally posted by @paul-dingemans in #2028 (comment)

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