reference Rule syntax

metavariable-type

See also
patterns, (TYPE $X)

An item of patterns. It keeps the matches where the expression bound to a metavariable has a given type. The value is a mapping:

The type of a variable comes from its declaration, such as the type of a parameter. A typed metavariable expresses the same condition within a pattern.

Examples

execute on a JDBC statement

statement-execute.yaml
rules:  - id: statement-execute    patterns:      - pattern: $X.execute($Q)      - metavariable-type:          metavariable: $X          type: java.sql.Statement    message: raw SQL executed on a java.sql.Statement    languages: [java]    severity: WARNING
statement-execute.java
import java.sql.Statement;import java.util.concurrent.Executor;class Jobs {    void run(Statement stmt, Executor pool, String query, Runnable task) {        // ruleid: statement-execute        stmt.execute(query);        // ok: statement-execute        pool.execute(task);    }}
Command and result
$ opengrep test .
1/1: ✓ All tests passed
No tests for fixes found.