typescript:S3801 false positive when a switch handles all variants

  • What language is this for?
    TypeScript

  • Which rule?
    typescript:S3801 - Functions should use “return” consistently

  • Why do you believe it’s a false-positive/false-negative?
    False positive, see the code below.
    Tested with TypeScript 5.0.4.

  • Are you using
    SonarQube - Enterprise Edition Version 9.9.1 (build 69595)

  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

export type Variant = "A" | "B" | "C";

// SQ: Refactor this function to use "return" consistently.
// But the default case is not needed here, since TS compiler already ensures that the switch returns something for every input variant.
export function getVariantNumber(variant: Variant): number {
    switch (variant) {
        case "A":
            return 1;
        case "B":
            return 2;
        case "C":
            return 3;
    }
}

Hello Jan,

Welcome to Sonar’s community, sorry for the long delay in answering you, and many thanks for the highly appreciated feedback!

As you rightfully pointed out, the code snippet you shared does indeed raise a false positive. I created this ticket to keep track of the problem and address it soon hopefully.

Best,
Yassin