diff --git a/loopy/symbolic.py b/loopy/symbolic.py
index 9309fd0418e453311240cce8e9f6c73a8d428f9c..09cce65cbc5a0c791224ed0c0c663ac30c811620 100644
--- a/loopy/symbolic.py
+++ b/loopy/symbolic.py
@@ -586,10 +586,10 @@ class ExpandingSubstitutionMapper(ExpandingIdentityMapper):
 
     def map_variable(self, expr, expn_state):
         result = self.subst_func(expr)
-        if result is not None or not self.within(expn_state.stack):
-            return result
-        else:
+        if result is None or not self.within(expn_state.stack):
             return ExpandingIdentityMapper.map_variable(self, expr, expn_state)
+        else:
+            return result
 
 # }}}