I get these sometimes too. I have two hadith to share with you that really helped me.
It is narrated on the authority of Abu Huraira that the Messenger of Allah (ﷺ) observed:
Verily Allah forgave my people the evil promptings which arise within their hearts as long as they did not speak about them or did not act upon them.
It is narrated on the authority of Abu Huraira that some people from amongst the Companions of the Apostle (ﷺ) came to him and said:
Verily we perceive in our minds that which every one of us considers it too grave to express. He (the Holy Prophet) said: Do you really perceive it? They said: Yes. Upon this he remarked: That is the faith manifest.
Realizing these are bad thoughts and therefore not acting upon them or speaking them is a sign of a person's faith. Verily Allah has already forgiven the ummah of Prophet Muhammad (ﷺ) for these thoughts.