The Prophet ﷺ said, "In the intimate relations of one of you there is charity."They asked, "Messenger of Allah, one of us fulfills his desire and receives a reward for it?"He replied, "Tell me, if he were to place it in what is forbidden, would he not bear a sin? Likewise, when he places it in what is lawful, he earns a reward."
عن النبي ﷺ قال: في بضع أحدكم صدقة قالوا: يا رسول الله! أيأتي أحدنا شهوته، ويكون له فيه أجر. فقال: أرأيتم لو وضعها في الحرام أكان عليه وزر. فكذلك إذا وضعها في الحلال، كان له أجر.
Muslim reported it in the Book of Zakat (1006), from Abdullah bin Muhammad bin Asma Dubai, who said, Mahdi bin Maymun narrated to us, who said, Wasil, the freed slave of Abu Uyayna, narrated to us, from Yahya bin Aqeel, from Yahya bin Yamar, from Abu Aswad Deeli, from Abu Dharr, and he mentioned it.I say, this hadith is a foundation for the existence of analogical reasoning in religion.