Hilchos Lashon Hara chapter 4, section 10
Even though it is forbidden to speak badly about such a person, it is allowed to tell one's son or students and warn them not to spend time with them, so they do not learn from their behavior.
The Torah forbids lashon hara when the speaker means to shame the other person and enjoy their disgrace. But if the speaker means to protect others from learning from that behavior, it is clearly allowed.
However, it is important to explain the reason to the listener – that it is being said as a warning. Then the listener will not wrongly think that speaking lashon hara is allowed, and will not be confused, thinking that the speaker is contradicting themselves.