saveourtool / diktat

Strict coding standard for Kotlin and a custom set of rules for detecting code smells, code style issues and bugs
https://diktat.saveourtool.com
MIT License
521 stars 39 forks source link

TOO_MANY_LINES_IN_LAMBDA when no lambda parameter #1007

Open cldfzn opened 3 years ago

cldfzn commented 3 years ago

Describe the bug

The following error occurs when using assertSoftly from kotest with a lambda > 10 lines. The function doesn't have a parameter so you cannot set a parameter name.

[TOO_MANY_LINES_IN_LAMBDA] long lambdas should have a parameter name instead of it: max length lambda without arguments is 10, but you have 13

Expected behavior

No error.

Observed behavior

[TOO_MANY_LINES_IN_LAMBDA] long lambdas should have a parameter name instead of it: max length lambda without arguments is 10, but you have 13

Steps to Reproduce

Use assertSoftly or another function with no parameters as a lambda with greater than 10 lines.

Environment information

petertrr commented 3 years ago

Hey @cldfzn , sorry for late response.

In this rule we actually have an additional check if it is used in lambda body: https://github.com/cqfn/diKTat/blob/5dd45745402973f4f3727786a25974d7d17db9ba/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter5/LambdaLengthRule.kt#L41

So in your case it seems that you are using assertSoftly inside another lambda with implicit parameter. Could you please confirm whether this is true? If so, you could make that outer lambda's parameter named and the issue should be resolved.

The warning message is indeed confusing and we will try to change it. Kotlin coding conventions suggest to add explicit parameter to outer lambda, so that it isn't shadowed, but I think that it makes sense in cases like yours too.

orchestr7 commented 2 years ago

Linked with https://github.com/analysis-dev/diktat/issues/1009

nulls commented 11 months ago

Test case:

fun test() {
    someValue?.let {
        assertSoftly {
            val test = ""
            val values = test.split(" ")
            val value0 = values[0]
            val value1 = values[1]
            val value2 = values[2]
            val value3 = values[3]
            val value4 = values[4]
            val value5 = values[5]
            val value6 = values[6]
            val value7 = values[7]
            val value8 = values[8]
            val value9 = values[9]

            value1 == value9
            // it == value8 -- leads to error
        }
    }
}

Even after #1009, we can't detect that it in inner lambda does relate to outer lambda