Al-Imām al-Bukhārī narrates that al-Ḥumaydī related to us; he said: Sufyān narrated to us; he said: Ismā’īl ibn Abī Khālid narrated to me, as did al-Zuhrī, who said:
I heard Qays ibn Abī Ḥāzim say: I heard ‘Abdullāh ibn Mas’ūd (may Allāh Almighty be content with him) say that the Prophet ﷺ said: “Envy (i.e. praiseworthy emulation) is permitted only in two cases: a man whom Allāh has given wealth and he spends it in rightful causes; and a man whom Allāh has given wisdom, and he judges according to it and teaches it to the people.”
حَدَّثَنَا الْحُمَيْدِيُّ قَالَ: حَدَّثَنَا سُفْيَانُ قَالَ: حَدَّثَنِي إِسْمَاعِيلُ بْنُ أَبِي خَالِدٍ - عَلَى غَيْرِ مَا حَدَّثَنَاهُ الزُّهْرِيُّ - قَالَ: سَمِعْتُ قَيْسَ بْنَ أَبِي حَازِمٍ قَالَ: سَمِعْتُ عَبْدَ اللهِ بْنَ مَسْعُودٍ قَالَ: قَالَ النَّبِيُّ ﷺ
لَا حَسَدَ إِلَّا فِي اثْنَتَيْنِ: رَجُلٌ آتَاهُ اللهُ مَالًا فَسُلِّطَ عَلَى هلَكَتِهِ فِي الْحَقِّ، وَرَجُلٌ آتَاهُ اللهُ الْحِكْمَةَ فَهُوَ يَقْضِي بِهَا وَيُعَلِّمُهَا.
Reference: Sahih Al Bukhari 73