diktat icon indicating copy to clipboard operation
diktat copied to clipboard

Unused arguments in lambda should not have types

Open orchestr7 opened this issue 3 years ago • 6 comments

This is a great stylistic bug, that is not handled by IDEA:

foo {_, _: Int, a: Int - > println("hi") }

diktat should detect and remove a type of such anonymous arguments (_: Int)

This should also be added to the code style

orchestr7 avatar Jan 17 '22 08:01 orchestr7

What if there are several overloads of function foo with the same number of arguments but different types? Then type of the lambda would be ambiguous. I guess it would make sense for named arguments, because I can't imagine a case, when its type cannot be inferred correctly.

petertrr avatar Jan 17 '22 08:01 petertrr

I guess it would make sense for named arguments

I meant only unnamed arguments

orchestr7 avatar Jan 24 '22 08:01 orchestr7

I meant some weird corner cases like

fun <T> foo(f: (a: T, b: String) -> Unit) = Unit
fun bar() {
    foo { _: Int, s -> println(s) }
}

Here removing : Int will cause compilation failure

petertrr avatar Feb 14 '22 16:02 petertrr

To give another example on built-in stdlib functions,

list.fold(null) { _: Any?, _ -> }

will not type-check if _: Any? is replaced by _.

ephemient avatar Feb 23 '22 21:02 ephemient

Hm, yes, that's true @petertrr @ephemient. But we need to cover at least simple cases somehow...

orchestr7 avatar Feb 24 '22 11:02 orchestr7

Without full type inference it is not possible to determine whether removing the explicit type keeps the same behavior of code or not.

ephemient avatar Feb 24 '22 12:02 ephemient