Interesting thought and depending on time period and culture I can imagine that. But this is not how city walls were described in classic greek text? (Case in point: Sparta, famously the only greek city without city walls, had tons of slaves) or how cities worked in medieval age (where "city air" freed you from serfdom).
https://en.wikipedia.org/wiki/Stadtluft_macht_frei
Also walls build on defensive terrain (hills) point to protection as its usage. There is the case that walls are useful to tax trade because you can funnel wares only through a gate which doubles as a custom house.