Separation or divorce raises many questions about residence permits, and the answers in online groups don’t always help. The first thing to understand is that the answer depends entirely on how you obtained your permit: whether you obtained it through work or through your partner.
If you are an EU citizen and hold a B permit because you work in Switzerland, divorce does not directly affect your situation. Your right of residence is linked to your employment, not your marital status. The Agreement on the Free Movement of Persons (AFMP) grants you that right in its own right. Getting divorced does not affect your permit as long as you continue to work.
The situation is different if you came to Switzerland as a "family member" of a European worker, without being in employment yourself. In that case, a divorce may complicate the renewal of your residence permit. It is not automatically withdrawn, but the canton will assess whether you have sufficient means of support or are in employment. The same applies to non-EU citizens: Article 50 of the Foreign Nationals Act (LEI) stipulates that the right to family reunification is maintained if the marriage lasted at least three years and there is sufficient integration, or if there are serious personal reasons justifying it.
In practice, anyone who arrived in Switzerland with their own employment contract and has been paying social security contributions for years is in a strong position. Anyone who arrived without a job, under spousal status, should consult the cantonal migration office before the divorce is finalised, not afterwards.