All perfect praise be to Allah, the Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad, sallallahu ‘alayhi wa sallam, is His slave and Messenger.
The hadeeth in reference was cited by both Al-Bukhaari and Muslim. The version of Al-Bukhaari reads, “Let him refrain from doing evil, as that, for him, is an act of charity.”
The version of Muslim reads, “I (Abu Tharr) said, ‘O Messenger of Allah, what if I cannot do some of these deeds?’ He replied, ‘Then desist from doing evil to people, for it is charity which you give to yourself.’”
Refraining from carrying out evil deeds is charity to people if he refrains from doing evil to them, and it is charity to oneself if the evil does not extend to others. Ibn Battaal wrote, “If someone asks: How is refraining from doing evil considered charity? The answer is: If he desists from doing evil to others, it is as if he gave them charity by sparing them his harm, and if the scope of his evil is confined to himself only, he has given himself charity by stopping it from committing sin.” [Sharh Saheeh Al-Bukhaari]
Allah knows best.