Skip to content

Fix S907 FP: Don't use goto case #9763

@DaleStan

Description

@DaleStan

Description

(False positive, I guess? This template fits better than the new rule template.) The existing S907 goto label rule also triggers on goto case/goto default.

I have a switch statement with (currently) 26 string cases, akin to the one below. For the sake of finding the labels and reducing merge conflicts when new cases are added, the case labels are in alphabetical order. This requires either code duplication or goto case statements. It would be nice to be able to configure goto case/goto default separately from goto label.

Reproducer

switch (insectType) {
    case "ant":
        goto case "termite";
    case "bee":
        goto case "wasp";
    case "termite":
        // handle ants and termites
        break;
    case "wasp":
        // handle bees and wasps
        break;
}

In my case, none of the case blocks with goto case contain any other code, but it might also be good to have a third setting for cases terminated with goto case, e.g.

    case "bee":
        // handle bee stuff that doesn't apply to wasps
        goto case "wasp";
    // ...
    case "wasp":
        // handle bees and wasps
        // (everything that applies to wasps also applies to bees)
        break;

Product and Version

SonarQube 8.27.0.14275 and Visual Studio 17.14.16

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions