github / github/codeql

CodeQL: Support multiple types for `instanceof`

Open
#8,662 3 comments 0 reactions 0 assignees View on GitHub
CLI
Dominant language
CodeQL
Stars
10.1k
Forks
2.1k
Avg merge
2d 15h
Merged PRs (30d)
141

Description

Currently `instanceof` only supports a single type as right operand. This can make its usage quite verbose when multiple alternative type checks have to be performed. For example search in the CodeQL source with the following Regex (increasing or decreasing the number in `{3,}` to find longer or shorter chains):
```
(.+) instanceof [a-zA-Z]+([\s\n]+or[\s\n]+\1 instanceof [a-zA-Z]+){3,}
```

This finds for example:
- https://github.com/github/codeql/blob/3119885a9b21951aafac88e01c946b3e7cfbe163/java/ql/lib/semmle/code/java/ControlFlowGraph.qll#L441-L451
- https://github.com/github/codeql/blob/3119885a9b21951aafac88e01c946b3e7cfbe163/java/ql/lib/semmle/code/java/JDK.qll#L157-L161

Would it be possible to allow multiple types for the `instanceof` expression? The syntax could be similar to a set literal, also allowing a trailing comma (except that currently a set literal is defined as expression, so it would not be valid for types), for example:
```codeql
this instanceof [PrimitiveType, NullType, VoidType, BoxedType, TypeString]
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.