metavariable-type
An item of patterns. It keeps the matches where the expression bound to a metavariable has a given type. The value is a mapping:
metavariable: the metavariable, bound by another item;type: the type, written as in the language of the rule. A short name such asStatementand a full name such asjava.sql.Statementare both accepted;types, instead oftype: a list of types; the expression may have any of them.
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: WARNINGstatement-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.