Moved too-early assignment inside the if stmt where the var is actually used. Close gh-1292.