Yet another interesting thought on this פסוק. Dr. Mehullam Klarberg discusses the issue with לו being masculine and apparently referring to עיר which is feminine:
Habayit asher ba'ir asher lo (spelled with Alef [meaning 'not'], but read lo spelled with Vav! meaning ['it has']) choma (Levit 25:30) ('the house which is in the city which has a wall'): The Masora says 'There are 16 written similarly with Alef and read with Vav.' Following both the ketiv ('written') and the kere ('read!') Torat Kohanim explains, 'Even though it does not now [have a wall] but it did have previously.' In a passage of Rashi (11th century) (in most editions this passage appears in brackets and the editor of Torat Chayim, Rabbi Ch. D. Chavel, points out that it did not appear in the first edition of Rashi), both the Masora and the Torat Kohanim are quoted and it is pointed out that the word ir ('city') is feminine so the pronoun referring to it should have been la (spelled with a mapik ('pronounced') Heh meaning ['she has']), and explains that because the written form is lo (spelled with Alef) they tikenu ('fixed') lo in the Masora, one form similar to the other form. According to this passage in Rashi, the Masora has abandoned the requirement for masculine-feminine agreement between a noun and its pronoun (which is obligatory in Hebrew), for the sake of 'one form similar to the other form!' Is it 'fixed' thereby? Research is required to determine the origin and status of this passage in Rashi.
Chizekuni (13th century) (who may or may not have been aware of the above passage in Rashi) is also concerned with the non-agreement of ir and lo. He writes that the word lo refers to sadeh ('field'), which is masculine and, he argues, is understood in the sentence even though it does not occur there. We have had discussion of words implied in sentences as explanations for apparent none agreement elsewhere (Morsels, Emor 5762).
Malbim (19th century) (who probably was aware of the above passage in Rashi and its problematic status) is also concerned with the non-agreement of ir and lo. He argues that lo refers to bayit. One should see the words asher ba'ir ('which is in the city') as parenthetical and read habayit . asher lo choma directly. According to Malbim the question disappears. There is no problem with lo.For my part, I'm not sure how the proposed alternative understandings of לו actually jive with what the פסוק is trying to say. A house that has a wall? What house doesn't have a wall? It is clear that we are talking about a walled city so how is it appropriate to attach the word לו to anything other than עיר?