cashapp/exhaustive

`@Exhaustive` doesn't work with `return when`

seanfreiburg opened this issue · 2 comments

For example: this does not compile with @Exhaustive. I use return when quite a bit and it cuts down on redundant returns. Android Studio also suggests me to do it, so it will be confusing when users try it and it won't compile.

This annotation is not applicable to target 'expression'

        @Exhaustive
        return when (viewAction) {
            is AppViewAction.SettingsClicked -> reduceOnSettingsClicked(dispatcher, previousState)
            is AppViewAction.CountUpdated -> reduceOnCountUpdated(previousState, viewAction.count)
            else -> previousState.copy()
        }

This is by design. When you use when as an expression (as forced by return) you get an exhaustive when by default. The annotation is only needed for statement form when there is otherwise no value being returned from the when.

TIL.... I did not know this behavior was a thing.