Fixes
A fix is a mechanical source edit that removes one violation. 47 rules have one; the Fix column of the index of rules and checks names each.
- In the editor, a fix is offered at its violation.
assay fixapplies every available fix of the project in one run.
Each fix is defined once, next to its rule, so both apply the same edit. A fix stays within the file of its violation and never removes behaviour. A fix that is not certain is not offered: each rule page lists the cases where its fix is not available, and there you fix the violation by hand.
Applying fixes with assay fix
Section titled “Applying fixes with assay fix”assay fix # every available fix in the projectassay fix lib/src # only at and below a pathassay fix --dry-run # print the edits, write nothingassay fix --rule no_commented_out_code # only this rule's fixes; repeatableThe command exists because the SDK’s bulk fix command does not apply fixes that come from a plugin. Every option and exit code is on assay fix.
╭─ assay fix ──────────────────────────────────────────────────────────────────╮│ Preset recommended ││ Rules 109 ││ Files 2 │╰──────────────────────────────────────────────────────────────────────────────╯
╭─ To fix ─────────────────────────────────────────────────────── 1 in 1 file ─╮│ Line Rule Fix ││ ───────────────────────────────────────────────────────────────────────── ││ lib/prices.dart 1 to fix ││ + 4:3 no_commented_out_code Delete the commented-out code ││ - 4 | // for (var index = 0; index < prices.length; index++) { ││ - 5 | // sum += prices[index]; ││ - 6 | // } │╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Dry run ────────────────────────────────────────────────────────────────────╮│ Would fix 1 violation in 1 file. Nothing was written. │╰──────────────────────────────────────────────────────────────────────────────╯╭─ assay fix ──────────────────────────────────────────────────────────────────╮│ Preset recommended ││ Rules 109 ││ Files 2 │╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Fixed ──────────────────────────────────────────────────────── 1 in 1 file ─╮│ Line Rule Fix ││ ───────────────────────────────────────────────────────────────────────── ││ lib/prices.dart 1 fixed ││ + 4:3 no_commented_out_code Delete the commented-out code │╰──────────────────────────────────────────────────────────────────────────────╯
╭─ ✓ Done ─────────────────────────────────────────────────────────────────────╮│ Fixed 1 violation in 1 file. │╰──────────────────────────────────────────────────────────────────────────────╯--rule with a rule that has no fix is a usage error that names the rules that have one.
How it writes:
- It judges the project first, with the same configuration as
check, then applies each file’s fixes to its text together and writes the file once. - Overlapping fixes are never both applied. The later one is left for the next run, and the output says so. An edit two fixes share, such as one
mountedcheck before twosetStatecalls, is written once. - Each file is written in one step: the new text goes under
.dart_tool/assay/, then is moved over the file. The file keeps its mode, byte order mark and line breaks. A file whose text changed since it was judged is not written. - The written files are judged again. A file that does not compile after its fixes gets its text from before them back, with the first compile error named. A fix must never leave the project broken.
A second run changes nothing, unless the first left fixes for the next run.
| Exit code | When |
|---|---|
| 0 | It could run. |
| 2 | A configuration or usage error. Nothing is written. |
| 3 | A file could not be written, or was left not compiling. |
What is never fixed
Section titled “What is never fixed”- A violation that a suppression hides, in either form.
- A file in an excluded path.
- A file that does not compile.
- A violation whose fix is not available (each rule page lists the cases).
- The results of the cross-file, asset and project-file checks: they have no fix.
- Anything by the gate. The gate never applies a fix. Its message marks the violations that have one with
[fix available]; runningassay fixis the agent’s or the person’s own act.
A violation the baseline knows is fixed like any other. fix never writes the baseline: the entry goes at the next check, gate or baseline update.
The fix of dispose_owned_resources has details of its own: a release that returns a future is written with unawaited, one fix releases every fixable resource of the State in one dispose, and a field handed to another object outside dispose can get one release too many. See its page.
In the editor
Section titled “In the editor”The editor offers a fix in its menu of actions at the violation. The keys that open the menu in VS Code, IntelliJ and Android Studio are on In the editor.