Formatter: Refactor the cast detection code to be a bit more readable.

No functionality change. Also add another cast test.