reference Flags

--autofix

Accepted by
opengrep scan
Also spelled
-a, --no-autofix
Default
false
See also
--dryrun, --replacement, fix, opengrep test, fix-regex

Writes the fixes of the rules to the files. A rule with a fix or fix-regex key describes the code that should replace each of its matches. The text output shows each change on a fix: line, with or without the flag; with the flag, the file is also changed.

The flag modifies the scanned files in place. The autofix mode is experimental. --dryrun shows the changes without writing them.

A fix can be tested before it is applied: opengrep test compares the result of the fixes with a <name>.fixed.<ext> file.

Examples

Rewriting a file

rule.yaml
rules:  - id: use-is-none    pattern: $X == None    fix: $X is None    message: use "is None"    languages: [python]    severity: WARNING
app.py
if x == None:    pass
Command and result
$ opengrep scan -q --config rule.yaml --autofix app.py
app.py

  warn  use-is-none
  use "is None"

     1: if x == None:
   fix: (x is None)
$ cat app.py
if (x is None):
    pass