Skip to content
Latchkey

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

Terminal
# 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/StringLiterals

Flags

FlagWhat it does
-a, --autocorrectApply safe autocorrections only
-A, --autocorrect-allApply all autocorrections, including unsafe
--only <cop>Restrict autocorrection to one cop
--disable-uncorrectableInsert 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.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →