Below is a screenshot of the official NBA rule book, rule 12A, section IV:
And here is a LeBron James dunk from last night:
Was Lebron hanging on the rim to "prevent an injury to himself or another player"?
No.
All the Celtics had stopped in their tracks. If LeBron doesn't grab the rim, he lands peacefully with everyone else far behind him. Hanging on the rim is what brought him back toward the other players. In fact, it almost caused an injury since it led to him kicking Marcus Smart in the head.