pinterest / ktlint

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

Conflicting `string-template-indent` and `function-signature` rules #2592

Closed szedelenyi closed 6 months ago

szedelenyi commented 7 months ago

Expected Behavior

Given the following config:

ktlint_function_signature_body_expression_wrapping = default

And the following code:

private fun buildSelectQuery(additionalWhereClause: String = "") =
    """
    SELECT
        $ID,
        $CREATED_AT,
        $CREATED_BY,
        $DELETED,
        $STATE
    FROM $SESSIONS
    WHERE $ID = :$ID
        AND $DELETED = false
        $additionalWhereClause
    """.trimIndent()

I would expect that the function-signature rule is ignored and the string-template-indent rule takes precedence. (Alternatively string-template-indent is ignored, but I feel that string-template-indent should take precedence.)

Observed Behavior

ktlint cannot format the given code, as it either violates function-signature or string-template-indent.

WARN com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine -- Format was not able to resolve all violations which (theoretically) can be autocorrected in file 

Steps to Reproduce

Run ktlint --format on the given snippet.

Your Environment

ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = unset ktlint_function_signature_body_expression_wrapping = default

as function signature body expression wrapping is set to default instead of multiline,

then it would be inconsistent to enable multiline expression wrapping

ktlint_standard_multiline-expression-wrapping = disabled


* Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): ktlint-cli 1.2.1
* Version of Gradle used (if applicable):
* Operating System and version: macOS 14.2.1
paul-dingemans commented 7 months ago

Tnx for reporting. This is a bug indeed.

For now you can suppress this as follows:

@Suppress("ktlint:standard:function-signature")
private fun buildSelectQuery(additionalWhereClause: String = "") =
    """
    SELECT
        $ID,
        $CREATED_AT,
        $CREATED_BY,
        $DELETED,
        $STATE
    FROM $SESSIONS
    WHERE $ID = :$ID
        AND $DELETED = false
        $additionalWhereClause
    """.trimIndent()
szedelenyi commented 7 months ago

Thanks, that's what I did already, but I thought it's worth flagging.