ac_matching
- Value
trueorfalse- Default
true- See also
commutative_boolop,symmetric_eq
a | b | c is parsed as (a | b) | c. With ac_matching, on by default, a chain of one associative operator is matched as a flat list of operands, not as the nested pairs the parser produces. The effect depends on the operator:
- The bitwise operators
|,&and^are also commutative: the operands of the pattern may appear in any order, among other operands.os.O_CREAT | os.O_TRUNCmatchesos.O_TRUNC | os.O_WRONLY | os.O_CREAT. - For
&&and||, the order is kept, but a metavariable can stand for several operands in a row:a() && $X && c()matchesa() && b() && d() && c().commutative_boolopremoves the order requirement as well.
With ac_matching: false, operators are matched as the parser nests them. commutative_boolop and symmetric_eq then have no effect.
Examples
Flags combined in any order
The rule trunc-create turns the option off.
flags.yaml
rules: - id: trunc-create-default pattern: os.O_CREAT | os.O_TRUNC message: creates or truncates the file languages: [python] severity: WARNING - id: trunc-create pattern: os.O_CREAT | os.O_TRUNC message: creates or truncates the file languages: [python] severity: WARNING options: ac_matching: falseflags.py
import osdef create(path): # ruleid: trunc-create-default, trunc-create os.open(path, os.O_CREAT | os.O_TRUNC) # ruleid: trunc-create-default os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) # ruleid: trunc-create-default os.open(path, os.O_TRUNC | os.O_WRONLY | os.O_CREAT) # ok: trunc-create-default os.open(path, os.O_CREAT | os.O_WRONLY)Command and result
$ opengrep test .
2/2: ✓ All tests passed
No tests for fixes found.A metavariable for several conditions
guard.yaml
rules: - id: guard-default pattern: user.active && $COND && user.admin message: admin check with extra conditions languages: [javascript] severity: WARNING - id: guard pattern: user.active && $COND && user.admin message: admin check with extra conditions languages: [javascript] severity: WARNING options: ac_matching: falseguard.js
// ruleid: guard-default, guardif (user.active && user.verified && user.admin) {}// ruleid: guard-defaultif (user.active && user.verified && req.secure && user.admin) {}Command and result
$ opengrep test .
2/2: ✓ All tests passed
No tests for fixes found.