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 — Useless type test — CodeQL query help documentation

Useless type test¶

ID: java/useless-type-test
Kind: problem
Security severity: 
Severity: warning
Precision: very-high
Tags:
   - quality
   - maintainability
   - useless-code
   - language-features
   - external/cwe/cwe-561
Query suites:
   - java-code-quality.qls
   - java-security-and-quality.qls

Click to see the query in the CodeQL repository

It is always the case that, for any type B, an instance of a type derived from B is also an instance of B. There is no need to explicitly test that this relationship exists.

Recommendation¶

Remove the unnecessary type test to simplify the code.

Example¶

The following example shows an unnecessary type test.

public class UselessTypeTest {
	private static class B {}
	private static class D extends B {}

	public static void main(String[] args) {
		D d = new D();
		if(d instanceof B) {	// violation
			System.out.println("Mon dieu, d is a B!");
		}
	}
}
  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402520 — 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.