Moved too-early assignment inside the if stmt where the var is actually used. Close gh-1292.
(cherry picked from commit 3a434434c420096cef478936ea21b33e9cfae30c)