Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Duplicate switch case

ID: js/duplicate-switch-caseKind: problemSecurity severity: Severity: warningPrecision: very-highTags:   - quality   - reliability   - correctness   - external/cwe/cwe-561Query suites:   - javascript-security-and-quality.qls

Click to see the query in the CodeQL repository

In JavaScript, cases in a switch statement can have arbitrary expressions as their labels. The interpreter does not check that these expressions are all different. At runtime, if two cases in a switch statement have the same label, the second case will never be executed. This most likely indicates a copy-paste error where the first case was copied and then not properly adjusted.

Recommendation

Examine the two cases to find out what they were meant to check. If both the case labels and their statements are identical, then the second case is duplicate code that can be deleted. Otherwise, the second case label needs to be adjusted.

Example

In the example below, the functioncontroller checks its parametermsg to determine what operation it is meant to perform. Note that the ‘switch’ statement has two cases labeled'start'; the second one will never be executed.

functioncontroller(msg){switch(msg){case'start':start();break;case'start':stop();break;default:thrownewError("Message not understood.");}}

Clearly, the second case should be labeled'stop':

functioncontroller(msg){switch(msg){case'start':start();break;case'stop':stop();break;default:thrownewError("Message not understood.");}}

[8]ページ先頭

©2009-2025 Movatter.jp