Skip to content

[Rule Proposal] Enforce (not) using a labelled return in (multiline) lambda bodies #3207

@FWest98

Description

@FWest98

Expected Rule behavior

The Kotlin Coding Conventions state:

Do not use a labeled return for the last statement in a lambda.

It would be nice to have a rule to check this. In addition, if possible, it would be nice to have a check for the opposite behaviour for multiline bodies specifically - this is the preferred style in our team but I have seen this in more places. Neither of these conflicts with IntelliJ formatting, since that does not touch labelled returns.

Concretely, I would expect the following behaviour:

// Accepted
fun foo() {
    val x = lambda {
        "value"
    }
}

// Rejected
fun foo() {
    val x = lambda {
        return@lambda "value"
    }
}

// Accepted when multiline bodies do not require a labelled return (default)
fun foo() {
    val x = lambda {
        doSomething()
        "value"
    }
}

// Accepted when multiline bodies do require a labelled return (configurable)
fun foo() {
    val x = lambda {
        doSomething()
        return@lambda "value"
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions