The Messenger of Allah ﷺ said, "Whoever Allah wishes good for, He grants him understanding of the religion."
حدثنا علي بن حجر حدثنا إسمعيل بن جعفر حدثني عبد الله بن سعيد بن أبي هند عن أبيه
أن رسول الله ﷺ قال من يرد الله به خيرا يفقهه في الدين
[Its isnad is sahih] Ahmad related it in 1/306 from the hadith of Ismail bin Jafar, through the same route.On this topic, there are also narrations from Umar [Tabarani in Al-Awsat, 4/175, no. 3312], Abu Hurairah [Ibn Majah, no. 220], and Muawiya [Bukhari, no. 71, and Muslim, no. 1037/98].