It's called a conflict of interest.
Specifically, when a lawyer already represents (or has previously represented) someone whose interests are adverse to a prospective client, that's a client conflict of interest — and the lawyer generally has to decline the case unless they can get informed written consent from both affected clients. If the conflict arises from the lawyer's own personal stake in the matter rather than another client, that's a personal conflict of interest, with the same general rule.
The duty comes from the ABA Model Rules of Professional Conduct (Rule 1.7 for direct conflicts and client consent, Rule 1.10 for conflicts that follow a lawyer between firms), and every state's bar has a matching version. Lawyers screen for these before taking on a new matter — the intake step is just called a conflicts check.