rubocop -A: Autocorrect Ruby Offenses
rubocop -a applies only safe autocorrections; rubocop -A also applies unsafe ones that may change behavior.
RuboCop can fix many offenses for you. The key choice is safe (-a) versus all including unsafe (-A), since unsafe fixes can alter semantics.
What it does
rubocop -a (--autocorrect) rewrites files for offenses whose fix RuboCop considers safe. rubocop -A (--autocorrect-all) additionally applies fixes marked unsafe, which can change runtime behavior, so review the diff.
Common usage
# safe corrections only
bundle exec rubocop -a
# all corrections, including unsafe
bundle exec rubocop -A
# autocorrect just one cop
bundle exec rubocop -A --only Style/StringLiteralsFlags
| Flag | What it does |
|---|---|
| -a, --autocorrect | Apply safe autocorrections only |
| -A, --autocorrect-all | Apply all autocorrections, including unsafe |
| --only <cop> | Restrict autocorrection to one cop |
| --disable-uncorrectable | Insert rubocop:disable for what cannot be fixed |
In CI
Do not run -A blindly in a gate job: unsafe corrections can change behavior, so the test suite must run after. A common pattern is a separate workflow that runs rubocop -A, then commits the result or opens a PR for review rather than auto-merging.
Common errors in CI
"An error occurred while Style/X cop was inspecting" during autocorrect usually points at a cop bug on unusual syntax; add the file to the cop's Exclude or report it upstream. After -A, a previously green test going red signals an unsafe correction changed behavior; revert and apply -a only.