Ruling on thinking of haraam things without actually doing them
If something crosses a person’s mind, such as thoughts of zina, stealing, consuming intoxicants and so on, but he does not do anything, then he is forgiven for that and he does not incur sin thereby, because the Prophet (peace and blessings of Allaah be upon him) said: “Allaah has forgiven my ummah for what crosses their minds, so long as they do not speak of it or act upon it.” And he (peace and blessings of Allaah be upon him) said: “Whoever thinks of doing a bad deed but does not do it, it will not be recorded against him;” and according to another version, “one hasanah (good deed) will be recorded for him, because he refrained from it for My sake.” Agreed upon – narrated from the hadeeth of Ibn ‘Abbaas (may Allaah be pleased with him). What this means is that if a person refrains from a bad deed that he was thinking of for the sake of Allaah, Allaah will record one hasanah for him, and if he refrains from it for any other reason, then no sayi’ah (bad deed) will be recorded against him, but no hasanah will be recorded for him either. This is the bounty of Allaah to His slaves; to Him be praise and thanks, there is no god but Him and no lord beside Him.
May Allaah send blessings and peace upon Muhammad and his family and companions. End quote.