-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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:
codeql/java/ql/lib/semmle/code/java/ControlFlowGraph.qll
Lines 441 to 451 in 3119885
this instanceof ReturnStmt or this instanceof ThrowStmt or this instanceof Literal or this instanceof TypeLiteral or this instanceof ThisAccess or this instanceof SuperAccess codeql/java/ql/lib/semmle/code/java/JDK.qll
Lines 157 to 161 in 3119885
this instanceof PrimitiveType or this instanceof NullType or this instanceof VoidType or this instanceof BoxedType or this instanceof TypeString
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:
this instanceof [PrimitiveType, NullType, VoidType, BoxedType, TypeString]