Only a Tzaddik is saved from forbidden thoughts:
One who suffers from forbidden thoughts and evil lusts that enter his mind is to know that this is the result of his animal soul which is impossible to control or prevent in anyone who is not a Tzaddik, on the Tanya level. Even a Beinoni who never sins in either thought speech or action still maintains an animal soul that sends thoughts of forbidden matters and lusts into his mind, including sexual thoughts of permitted or forbidden relations. The purpose of the thoughts entering one’s mind is for him to control and repress them, not allowing them to be entertained. By doing so one fulfills the negative command of “Lo Sassuru Acharei Levavchem Veacharei Eiyneichem Asher Ayem Zonim Achareihem”, and receives reward as if he fulfilled a Mitzvah.
 Tanya chapter 27