This sort of thing happens time and time again, a friend of ours, an academic working on a Home Office research project who was legally in this country took and passed his driving test, for some reason giving him a UK driving licence meant checking on his immigration status, and the Home Office got it wrong and said he was here illegally. Although the university he worked for knew he was here legally they had to suspend him without pay and if it hadn't been for the local MP and his university boss he would have been deported.
Finally after six months, the Home office admitted it had made a mistake, that he was here legally, and he resumed his research work, burdened with a heavy debt to all his friends who had lent him money and provided a roof over his head when he was penniless.
Mind you had he committed a crime, the Home Office would have given him permission to stay because of some technical reason why he could not be deported