The Messenger of Allah ﷺ said, "Allah has pardoned my community for whatever crosses their minds, so long as they do not speak of it or act upon it."
حدثنا قتيبة حدثنا أبو عوانة عن قتادة عن زرارة بن أوفى
قال رسول الله ﷺ تجاوز الله لأمتي ما حدثت به أنفسها ما لم تكلم به أو تعمل به
Agreed upon [by Bukhari and Muslim]. Muslim also reported it in the Book of Faith, chapter on Allah overlooking passing thoughts and fleeting notions in the heart when they do not settle, hadith no. 127, from Qutayba, and Bukhari, hadith no. 5269, from the hadith of Qatada, with this wording.