In the ideal case, all casts of literals would be flattened to literals, so this code would not be needed. So clearly we are not dealing with the ideal case. So let's figure out the exact circumstances where a cast of a literal would remain.
One possibility is a cast of a date string (e.g. '2010-01-01') to a timestamp. That's not a "pure" cast because it needs a timezone from the executor.
Another possibility is a cast of a non-nullable timestamp literal to a nullable timestamp.
Based on what you've said, the latter seems more likely. I think you should narrow your code down to the exact kind of cast (e.g. one that converts a timestamp to a timestamp and changes nullability but does not change data type). Otherwise there's too much risk of unintended consequences.