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 — Unused label — CodeQL query help documentation

Unused label¶

ID: java/unused-label
Kind: problem
Security severity: 
Severity: recommendation
Precision: high
Tags:
   - quality
   - maintainability
   - useless-code
   - external/cwe/cwe-561
Query suites:
   - java-code-quality.qls
   - java-security-and-quality.qls

Click to see the query in the CodeQL repository

Loop and switch statements can be labeled. These labels can serve as targets for break or continue statements, to specify which loop or switch statement they refer to.

Apart from serving as such jump targets, the labels have no effect on program behavior, which means that having an unused label is suspicious.

Recommendation¶

If the label is used to document the intended behavior of a loop or switch statement, remove it. It is better to use comments for this purpose. However, an unused label may indicate that something is wrong: that some of the nested break or continue statements should be using the label. In this case, the current control flow is probably wrong, and you should adjust some jumps to use the label after checking the desired behavior.

Example¶

The following example uses a loop and a nested loop to check whether any of the currently active shopping carts contains a particular item. On line 4, the carts: label is unused. Inspecting the code, we can see that the break statement on line 10 is inefficient because it only breaks out of the nested loop. It could in fact break out of the outer loop, which should improve performance in common cases. By changing the statement on line 10 to read break carts;, the label is no longer unused and we improve the code.

public class WebStore {
	public boolean itemIsBeingBought(Item item) {
		boolean found = false;
		carts:  // AVOID: Unused label
		for (int i = 0; i < carts.size(); i++) {
			Cart cart = carts.get(i);
			for (int j = 0; j < cart.numItems(); j++) {
				if (item.equals(cart.getItem(j))) {
					found = true;
					break;
				}
			}
		}
		return found;
	}
}

References¶

  • Help - Eclipse Platform: Java Compiler Errors/Warnings Preferences.

  • Common Weakness Enumeration: CWE-561.

  • © 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.