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 — Unread local variable — CodeQL query help documentation

Unread local variable¶

ID: java/local-variable-is-never-read
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

A local variable that is never read is useless.

As a matter of good practice, there should be no unused or useless code. It makes the program more difficult to understand and maintain, and can waste a programmer’s time.

Recommendation¶

This rule applies to variables that are never used as well as variables that are only written to but never read. In both cases, ensure that no operations are missing that would use the local variable. If appropriate, simply remove the declaration. However, if the variable is written to, ensure that any side-effects in the assignments are retained. (For further details, see the example.)

Example¶

In the following example, the local variable oldQuantity is assigned a value but never read. In the fixed version of the example, the variable is removed but the call to items.put in the assignment is retained.

// Version containing unread local variable
public class Cart {
	private Map<Item, Integer> items = ...;
	public void add(Item i) {
		Integer quantity = items.get(i);
		if (quantity = null)
			quantity = 1;
		else
			quantity++;
		Integer oldQuantity = items.put(i, quantity);  // AVOID: Unread local variable
	}
}

// Version with unread local variable removed
public class Cart {
	private Map<Item, Integer> items = ...;
	public void add(Item i) {
		Integer quantity = items.get(i);
		if (quantity = null)
			quantity = 1;
		else
			quantity++;
		items.put(i, quantity);
	}
}

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.