Backport Add Cast() case in OptimizerCore.pretransformExpr().

This is necessary for a Cast(expr) not to force expr if the
result of the Cast is not used.

(cherry picked from commit 12a2509a67485d4db9c9db1c027ca268e43bfd1d)