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
codeql/java/ql/lib/semmle/code/java/JDK.qll
Lines 157 to 161 in 3119885
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]