the texts of al-Shāfiʿī and his associates all concur that building a mosque over a grave is disliked, whether the deceased was renowned for piety or otherwise, because the ḥadiths are general in their wording.
I say: with them, this “dislike” is one of tanzīh (non-prohibitive dislike). An indication of that is their further statement: “It is disliked to pray over a grave.” Al-Nawawī commented:
“This is what they have said—‘it is disliked’. Yet were someone to say that it is forbidden, on account of the ḥadith of Abū Marthad and the others cited above, it would not be far-fetched.”
Had al-Nawawī—may Allah have mercy on him—made a similar remark about his colleagues’ view that construction is merely disliked, it would have been even more fitting, for the prohibition of building is more severe and more emphatic than that of praying toward a grave, as anyone who has examined the relevant ḥadiths will recognise.
For this reason Shaykh al-Islām said in Iqtiḍāʾ:
“As for building mosques over graves, the great majority of groups have expressly forbidden it in obedience to the ḥadiths. Our own Ḥanbalī scholars, together with many among the Mālikīs, Shāfiʿīs, and others, have declared it prohibited. Some scholars have used the term ‘disliked’ with regard to it—he is apparently referring to al-Shāfiʿī—but I do not know whether they intended tanzīh or taḥrīm. In any case, there is no doubt that it is definitively forbidden.”
He then cited the ḥadiths reported on this topic.
Al-Qurṭubī, in his Tafsīr, summed up the matter as follows:
“Turning graves into mosques, praying in them, building over them, and the like—all of which the Sunnah has forbidden—are acts that are not permitted.”