GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Non-case label in switch statement — CodeQL query help documentation

Non-case label in switch statement¶

ID: java/label-in-switch
Kind: problem
Security severity: 
Severity: recommendation
Precision: very-high
Tags:
   - quality
   - maintainability
   - readability
Query suites:
   - java-code-quality.qls

Click to see the query in the CodeQL repository

Overview¶

Java allows to freely mix case labels and ordinary statement labels in the body of a switch statement. However, this is confusing to read and may be the result of a typo.

Recommendation¶

Examine the non-case labels to see whether they were meant to be case labels. If not, consider placing the non-case label headed code into a function, and use a function call inline in the switch body instead.

Example¶

public class Test {
    void test_noncase_label_in_switch(int p) {
        switch (p) {
            case 1: // Compliant
            case2:  // Non-compliant, likely a typo
            break;
            case 3:
                notcaselabel: // Non-compliant, confusing to read
                for (;;) {
                    break notcaselabel;
                }
        }
    }
}

In the example, case2 is most likely a typo and should be fixed. For the intentional notcaselabel, placing the labelled code into a function and then calling that function is more readable.

References¶

CodeQL query help for JavaScript and TypeScript - Non-case label in switch statement.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.