The two reports that answer it directly
The first removes the sin from the thought, in plain terms, and it is in Bukhari. The second is the one worth reading twice. Companions came saying they found something in themselves too grave to say out loud. He asked whether they had really experienced that. They said yes. He said that is clear faith.
Qur’an and Sunnah
Narrated Abu Huraira:The Prophet (ﷺ) said, "Allah has forgiven my followers the evil thoughts that occur to their minds, as long as such thoughts are not put into action or uttered." And Qatada said, "If someone divorces his wife just in his mind, such an unuttered divorce has no effect.:
Sahih al-Bukhari 5269graded sound, sahihaynfull report
Abu Hurairah said; His companion came to him and said; Messenger of Allah! We have thoughts which we cannot dare talk about and we do not like that we have them or talk about them. He said:Have you experienced that? They replied: yes. He said : that is clear faith
Sunan Abi Dawud 5111graded sound, albanifull report