https://kotlinlang.org logo
#detekt
Title
# detekt
d

dimsuz

03/24/2023, 4:30 PM
Hi! Is there an existing rule that would report if some
also
is unnecessary, for example
Copy code
foo.also { bar() } // doesn't use "it"
l

Landry Norris

03/24/2023, 4:35 PM
I often use this with expressions. Sometimes I just need
Copy code
fun foo() = Bar().also { baz() }
With this rule, it'd make me use a full function body.
Typically baz would use the new Bar in some way, needing 'it', but not always.
d

dimsuz

03/24/2023, 5:57 PM
Ah, right, thats a nice use case. I've just found
Copy code
fun foo() {
  val x = 3
  bar().also { baz() }
}
in our code and thought it makes no sense. I guess it was using
it
at some point, which got lost after some edit.
I may try to write a rule for this which would exclude your case.
l

Landry Norris

03/24/2023, 6:34 PM
A rule like this with an option to ignore expressions makes sense. Expressions are the only case I can think of where this matters.
a

Atul Gupta

03/26/2023, 7:21 PM
Hi @Landry Norris / @dimsuz can you create a new issue in the repo(if not already created)?
d

dimsuz

03/27/2023, 5:02 PM
I thought I'd try to implement + apply this rule in the local ruleset to test it out...
a

Atul Gupta

03/28/2023, 10:48 AM
That's more better. Than also push it to
detekt
😀
3 Views