Use filters to refine rule matches
masterFilters are used within a Where() call to reduce false positives. A rule matches only if an AST pattern is found and all filters in Where() return true.
Submatch Filters
Access named pattern variables using m["varname"]. Common filters include:
m["a"].Type.Is("T"): Submatch type is identical toT.m["a"].Type.AssignableTo("T"): Submatch type is assignable toT.m["a"].Type.Implements("I"): Submatch type implements interfaceI.m["a"].Const: Checks if the submatch is a constant expression.m["a"].Text.Matches("regexp"): Submatch text matches a regex.
Context Filters
Apply filters based on the file context using the m object:
m.File().Imports("package/path"): Checks if the current file imports a specific package.
Note: When using MatchComment, submatch variables have the type *ast.Comment.
// Example: $a must be int, and $b must NOT be assignable to []string
Where(m["a"].Type.Is(`int`) && !m["b"].Type.AssignableTo(`[]string`))
// Example: File-level filter
Where(m.File().Imports("io/ioutil"))