v2.autofix #
v2.autofix
Mechanical source-level rewrites driven by the -autofix flag.
Currently implemented
-
Add missing
mut: A variable declared with:=that is later mutated (x = ...,x += ...,x++,x--, or passed asmut xto a call) is rewritten tomut x := .... Lexical scope is honored, so a shadowed innerxdoes not affect the outer declaration.Skipped for declarations in
for-init(for i := 0; ... ; i++) where V treats the loop variable as implicitly mutable.
Candidates for future autofix passes
These are mechanical, low-risk rewrites that fit the same style. Listed here so we can pick them up later, not implemented yet.
- Remove unused
mut: variable declaredmut x := ...but never mutated → drop themutkeyword. - Unused imports:
import foowith no reference tofoo.*in the file → delete the import line. - Unused local variables:
x := exprwherexis never read → rename to_(whenexprhas side effects) or delete the statement (whenexpris pure). - Unnecessary
unsafe { ... }:unsafeblock whose body contains no unsafe operations → unwrap. - Unused function parameters: warn-only today; autofix could prefix with
_to silence the diagnostic. - Snake_case rename: function/variable names in camelCase → rename to snake_case (with cross-file references updated).
- Trailing-comma normalization: enforce or strip the trailing comma in multi-line lists according to the project style.
- Add
?/!propagation: when a return value is an Option/Result and the caller ignores it via_ := f(), suggestf()?orf()!based on the enclosing function's return type. - Replace
if x != none { x.foo() }withif y := x { y.foo() }for Option chains. println('${x}')→println(x)when the interpolation has no formatting and the value is already a string.- Drop redundant
returnat the end of avoidfunction. - Re-order
pub/mutmodifiers to match the canonical order V fmt uses (pub mut:etc.).
fn run #
fn run(files []ast.File, file_set &token.FileSet) int
run scans the given files and, when fixes are found, rewrites the corresponding source files in place. Returns the number of edits applied.