Fixes: https://github.com/llvm/llvm-project/issues/53354
Added test.
NIT: maybe keep a single if statement and use dyn_cast_or_null?
why not && true!?
Addressed comments.
LGTM, thanks!
NIT: maybe keep a single if statement and use dyn_cast_or_null?