Backport of core [[https://github.com/bitcoin/bitcoin/pull/17474 | PR17474]].
Only the second commit is relevant:
https://github.com/bitcoin/bitcoin/pull/17474/commits/4341bffb6ef10909f3721329db27c5dc9bc720dd
This has been adapted to match our codebase changes from core. Due to
these changes the switch needs to have a default statement or the
compiler will always warn, so this backport does not change the
behavior;