. I don't think there's a Detekt rule that checks dangling objects (or just never written this kind of code). @chao afaiu the use case is:
Copy code
class SomethingC
object SomethingO
class Other {
fun f() {
this.state = SomethingO // ok, SomethingO.INSTANCE is used
this.state = SomethingC() // ok, constructor's "return value" is used
SomethingO // flag
SomethingC() // flag
}
}
twisterrob
03/30/2023, 3:10 PM
@Kirill Zhukov I think you can write your own rule pretty easily with type resolution. Find all the constructor calls / object references to classes extending state, and then validate that they're part of an assignment, a function call, or return statement/expression. The only thing I don't know how to detect is
.let { MyState(it) }
or
when { true -> MyState; else -> error() }
, because these usages are not an assignment, nor a function call.
k
Kirill Zhukov
04/03/2023, 5:15 PM
That’s right, ideally I would want something similar to