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;
}
}
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.
This issue is really annoying. Is there anything one could help to fix it?
I noticed that there is now a second issue created in Jira: JS-505 next to JS-90, which could be marked as dup by someone with write access to Jira
In cases where a function should handle all cases of e.g. a enum within a switch, we actually prefer to have no default return, to ensure in case the enum might be modified (which might come form an external API) we get a a compile error. So at the moment, I quite often mark those cases as false positives.