All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad is His slave and Messenger.
It was authentically reported that the Prophet, sallallaahu ‘alayhi wa sallam, swore by Allaah. It was also narrated through an authentic Hadeeth that he said: "Whoever has to swear, let him swear by Allaah. Do not swear by your fathers." [Al-Bukhari and Muslim]
The Hadeeth means – Allaah Knows best – that when one wants to swear, he should not swear by his father or mother or any thing other than Allaah The Almighty. The one who does not want to swear, however, is not addressed by the Hadeeth.
Ash-Shaafi‘i did not want to swear, as a principle, in order to keep the Name of Allaah Exalted and Venerated, out of compliance to the verse where Allaah Says (what means): {And do not make [your oath by] Allaah an excuse against being righteous and fearing Allaah and making peace among people.} [Quran 2: 224]
The scholars of Tafseer (Exegesis of the Quran) said that the meaning of the verse is: "Do not make much of oaths by Allaah, even if you say the truth and intend reform, for making many oaths by Allaah figures an audacious attitude toward Him.
What you mentioned about Ash-Shaafi‘i of his refraining from swearing is reported by many scholars as one of his virtues. Abu Nu‘aym Al-Asbahaani narrated from Ar-Rabee‘ ibn Sulaymaan that he said: “I heard Ash-Shaafi‘i saying, 'I never swore by Allaah, whether on something true or false.'” [Hilyat Al-Awliyaa’]
This reflects his prudence, deep understanding of religion and exaltation of Allaah The Almighty. This, however, does not mean that he regarded swearing by Allaah as forbidden or that he did not swear although he needed to. Rather, it means that he did not swear, in all cases, out of exaltation for Allaah and also because he did not need to swear.
Allaah Knows best.