كتاب الأيمان
27  
The Book of Oaths
(3)
Chapter: It is recommended for the one who swears an oath then sees that something else is better than it; To do that which is better and offer expiation for his oath
(3)
باب نَدْبِ مَنْ حَلَفَ يَمِينًا فَرَأَى غَيْرَهَا خَيْرًا مِنْهَا أَنْ يَأْتِيَ الَّذِي هُوَ خَيْرٌ وَيُكَفِّرَ عَنْ يَمِينِهِ
Sahih Muslim 1651 a

Tamim b. Tarafa reported:

A beggar came to 'Adi b. Hatim and he begged him to give him the price of a slave, or some portion of the price of the slave. He ('Adi) said: I have nothing to give you except my coat-of-mail and helmet. I will, however, write to my family to give that to you, but he did not agree to that. Thereupon 'Adi was enraged, and said: By Allah, I will not give you anything. The person (then) agreed to accept that, whereupon he said: By Allah, had I not heard Allah's Messenger (ﷺ) saying:" He who took an oath, but then found something more pious in the sight of Allah, he should (break the oath) and do that which is more pious," I would not have broken the oath (and thus paid you anything).
حَدَّثَنَا قُتَيْبَةُ بْنُ سَعِيدٍ، حَدَّثَنَا جَرِيرٌ، عَنْ عَبْدِ الْعَزِيزِ، - يَعْنِي ابْنَ رُفَيْعٍ - عَنْ تَمِيمِ بْنِ طَرَفَةَ، قَالَ جَاءَ سَائِلٌ إِلَى عَدِيِّ بْنِ حَاتِمٍ فَسَأَلَهُ نَفَقَةً فِي ثَمَنِ خَادِمٍ أَوْ فِي بَعْضِ ثَمَنِ خَادِمٍ ‏.‏ فَقَالَ لَيْسَ عِنْدِي مَا أُعْطِيكَ إِلاَّ دِرْعِي وَمِغْفَرِي فَأَكْتُبُ إِلَى أَهْلِي أَنْ يُعْطُوكَهَا ‏.‏ قَالَ فَلَمْ يَرْضَ فَغَضِبَ عَدِيٌّ فَقَالَ أَمَا وَاللَّهِ لاَ أُعْطِيكَ شَيْئًا ثُمَّ إِنَّ الرَّجُلَ رَضِيَ فَقَالَ أَمَا وَاللَّهِ لَوْلاَ أَنِّي سَمِعْتُ رَسُولَ اللَّهِ صلى الله عليه وسلم يَقُولُ ‏ "‏ مَنْ حَلَفَ عَلَى يَمِينٍ ثُمَّ رَأَى أَتْقَى لِلَّهِ مِنْهَا فَلْيَأْتِ التَّقْوَى ‏"‏ ‏.‏ مَا حَنَّثْتُ يَمِينِي ‏.‏
Reference : Sahih Muslim 1651a
In-book reference : Book 27, Hadith 22
USC-MSA web (English) reference : Book 15, Hadith 4056
  (deprecated numbering scheme)