Skip to content

Conversation

@kunalrkak
Copy link

@kunalrkak kunalrkak commented Nov 12, 2025

Before this PR

Add an errorprone check to steer developers away from using instanceof checks with sealed union types, instead pushing them towards switches and visitors, which are generally more efficient and also force exhaustive handling of all types.

After this PR

==COMMIT_MSG==
Adds an errorprone check to flag instanceof check usage with sealed union types.
==COMMIT_MSG==

Possible downsides?

@pkoenig10
Copy link
Member

This feels a little misguided. Given the existence of default cases, I don't think this check adds meaningful protection. There's not really a difference between these two implementations, yet this check would disallow the former and allow the latter.

if (value instanceof Foo foo) {
    // do something
} else {
    // do something else
}
switch (value) {
    case Foo foo -> {
        // do something
    },
    default -> {
        // do something else
    }
}

It's pretty common to have code that wants to handle exactly one case of a union and using a switch statement is less ergonomic for these cases. Another common pattern I see is "return if not instanceof":

if (!(value instanceof Foo foo)) {
    // return early
}

// do something with foo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants