Thus, someone who actively promotes his deviation is barred from holding office, leading the prayer, giving testimony, and transmitting hadith, because this constitutes forbidding wrongdoing—not because the prayer is invalid or because his testimony and narrations are suspect. If a person can avoid appointing someone who openly displays wrongdoing to lead the prayer, he must do so.
However, if someone else has appointed him and one cannot remove him from leading the prayer, or can remove him only at the cost of an evil more harmful than the wrongdoing he openly displays, then it is not permissible to remedy a lesser evil with a greater one, or to avert the lesser of two harms by bringing about the greater. For the Sharia came to secure and enhance benefits, and to eliminate and reduce harms as far as possible. Its aim is to give precedence to the greater of two goods when both cannot be attained, and to avert the worse of two evils when both cannot be averted.
If preventing someone who openly displays religious innovation and immorality from leading the prayer would cause greater harm than his leadership, doing so is not permissible. Rather, one should pray behind him those prayers that one cannot perform except behind him, such as Friday prayers, Eid prayers, and congregational prayers, when no other imam is available.
This is why the Companions would perform Friday and congregational prayers behind al-Hajjāj, al-Mukhtār ibn Abī ʿUbayd al-Thaqafī, and others. Missing Friday and congregational prayers is a greater evil than following an immoral imam in them, especially when staying away does not put an end to his immorality. In that case, a benefit prescribed by the Sharia is forfeited without that evil being averted. For this reason, those who categorically abandon Friday and congregational prayers behind unjust imams were regarded by