Fixes https://bugs.llvm.org/show_bug.cgi?id=51790. The check triggers incorrectly with non-type template parameters.
A bisect determined that the bug was introduced here:
https://github.com/llvm/llvm-project/commit/ea2225a10be986d226e041d20d36dff17e78daed
Unfortunately that patch can no longer be reverted on top of the main branch, so add a fix instead. Add a unit test to avoid regression in the future.