Given code like this: ```enum class Foo { Bar,...
# intellij
w
Given code like this:
Copy code
enum class Foo {
    Bar,
    Baz,
}

fun foo(foo: Foo) = when (foo) {
    Foo.Bar -> 2
    Foo.Baz -> 3
}

fun main() {
    println(foo(Foo.Bar))
}
Is there any inspection I can run to find out that
Foo.Baz
can be removed? I want to find actually unused code, and
Foo.Baz
is effectively unreachable if nothing ever uses
Foo.Bar
as value parameter (?) or assignment value
c
I don't think that's true. Code could be using
Foo.entries()
. If this is a library, some external code could be using it too.
w
If this is a library, some external code could be using it too.
that's fair, but I still want to run it if I know all my modules are self-contained if I'm developing an application, or if the identifiers are not public
Code could be using
Foo.entries()
That's right 😕 Same for sealed classes and reflection I guess, but I'd still want to find declarations without explicit usage. Technically it might not be correct, but it'd still be useful
c
You could run ‘find usage’ on Baz, no?
w
I can but I have to run it for every declaration, look at the results and determine that all are just reads. I want to run this on entire enum class with ~40 entries for example, and find all entries that I can potentially remove
c
if you make fun foo
private
you will get a notification “value of parameter foo is alway Foo.Bar”
then you can remove that case and then Foo.Baz is unused
w
I don't see that warning 👀 But regardless this wouldn't work as soon as I have more entries:
Copy code
private enum class Foo {
    Bar,
    Baz,
    Baa,
}

private fun foo(foo: Foo) = when (foo) {
    Foo.Bar -> 2
    Foo.Baz -> 3
    Foo.Baa -> 4
}

private fun main() {
    println(foo(Foo.Bar))
    println(foo(Foo.Baa))
}
c
i pasted it into a .kt file maybe thats the difference. but you are right!
w
I wanted to keep the example simple, in reality I have an
enum class
or a
sealed interfaces
with ~20-40 entries, and those entries are all mapped to something else in one function. Then application code passes these enum/interface classes somewhere, but at some point some of the entries might stop being used. They'll still be mapped so technically used as far as IDE is concerned, but I want to find candidates to remove
And of course it's not one enum, it's dozens of enums with dozens of entries, so I really need something automated
c
idea has a warning “value is written but never read”, and I think there should be something like this for your case
but i am not sure how to call it
w
I only see this one, but yeah that's what I want. Or rather the opposite — value is read (in a
when
) but never written (passed)
c
maybe file a youtrack with a feature idea?
w
Yeah I think I will. I just hoped something exists already 😄