The Prophet ﷺ said, "Allah has pardoned my community for whatever passes through their minds, so long as they do not speak of it or act upon it."
أن النبي ﷺ قال: إن الله تجاوز لأمتي ما حدثت به أنفسها ما لم يتكلموا أو يعملوا.
Bukhari narrated it in the Book of Emancipation (2528), and Muslim in the Book of Faith (127), both of them from the hadith of Masaar, from Qatada, from Zurara bin Awfa, from Abu Hurairah, who mentioned it.