Legal loophole exploited by developers means 80% of affordable homes lost