There is a law about it.
web.archive.org/…/qualified-immunity-supreme-cour…
And if the 1982 SCOTUS had been given the full text of the relevant law, then QI would have never happened. It is expressly illegal according to the full text of Section 1983