diktat icon indicating copy to clipboard operation
diktat copied to clipboard

TOO_MANY_LINES_IN_LAMBDA when no lambda parameter

Open cldfzn opened this issue 4 years ago • 3 comments

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

  • diktat version:n 1.0.0-rc.2
  • build tool (maven/gradle): gradle
  • how is diktat run (CLI, plugin, etc.): plugin
  • kotlin version: 1.5.21
  • operating system: macOS
  • link to a project (if your project is public):

cldfzn avatar Jul 28 '21 20:07 cldfzn

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.

petertrr avatar Aug 13 '21 08:08 petertrr

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

orchestr7 avatar Mar 02 '22 20:03 orchestr7

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

nulls avatar Sep 20 '23 10:09 nulls