The kavach Command: fix
Also known as: autofix auto fix lint dead code unreachable eslint --fix cargo fix clippy --fix ruff --fix
● Intermediate2 minBefore this: The kavach Command: check
kavach fix makes the changes that have one right answer, in every file you name, and says what it changed.
fn grade(score: Int) -> String {
if score > 50 {
"pass"
} else if score > 80 {
"great"
} else {
"try again"
}
}
fn medal(place: Int) -> String {
match place {
1 => "gold",
_ => "none",
2 => "silver",
}
}
fn main() {
print(grade(90))
print(medal(2))
}kavach run
pass
none
warning: this condition is never true here: `score > 80` means `score > 50`, and that branch is taken above. Check the order of the conditions (spec 4.7)
--> src/main.kav:4:15
|
4 | } else if score > 80 {
| ^^^^^^^^^^
warning: this arm never matches: the arm above it matches every value first. Remove it, or move it up (spec 4.7)
--> src/main.kav:15:9
|
15 | 2 => "silver",
| ^kavach fix src
src/main.kav: Remove this branch src/main.kav: Remove this arm
kavach fix src
nothing to fix
kavach run
pass none
- The run warns twice: an else if that can never be true, and a match arm that can never match. Each has one right fix: take it out.
kavach fix srctakes out both and prints one line per fix. Run again, there isnothing to fix, and the program runs without warnings.- The
elseafter the removed branch stays, joined to the branch before, sogradestill has an answer for every score.
Errors it fixes too
A program that doesn't compile can be fixed the same way, when each error has one right answer:
fn main() {
const counts = [3, 4, 7]
counts.push(8)
for i in 0..=counts.len() {
if counts[i] = 4 {
print("four")
}
if counts[i] % 2 == 1 {
print("{counts[i]} is odd")
}
}
}kavach check
error: `=` sets a variable: to compare, write `==` (spec 4.1)
--> src/main.kav:5:22
|
5 | if counts[i] = 4 {
| ^kavach fix src
src/main.kav: Write `==` src/main.kav: Declare it with `let` src/main.kav: Write `..` instead of `..=` src/main.kav: Write `counts[i] % 2 != 0`
kavach run
3 is odd four 7 is odd
kavach check shows only the syntax error, since a file with syntax errors gets only those. kavach fix still makes all 4 fixes, and the program runs. Everything kavach fix does today:
| The mistake | What it writes |
|---|---|
| An else if or a match arm that never runs | takes it out |
| A call to a deprecated function | the call its fix says |
= in a condition | == |
0..=xs.len() reading xs[i] | 0..xs.len() |
x % 2 == 1, the odd test that is wrong for negative numbers | x % 2 != 0 |
A const that is changed | let |
A pub function that uses a capability without saying so | the uses it needs, such as uses Fs.read |
- It never makes an edit that adds an error. A fix that wouldn't compile where it is, such as a replacement that isn't imported there, is left, with its message.
- A fix that may be wrong is left to you. A misspelled name has a closest match, but it may not be the one you meant, so only the editor offers "Change to
total", and you choose. The same holds for aletthat hides another variable. - The editor offers every fix one at a time, as quick fixes: see Editor Setup.
- Which everyday mistakes it fixes, measured: Mistakes Caught Before a Run.