Skip to content
Kavachकवच

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))
}
src/main.kav, before: Kavach code
terminal
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 src takes out both and prints one line per fix. Run again, there is nothing to fix, and the program runs without warnings.
  • The else after the removed branch stays, joined to the branch before, so grade still 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")
        }
    }
}
src/main.kav, before: Kavach code
terminal
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 mistakeWhat it writes
An else if or a match arm that never runstakes it out
A call to a deprecated functionthe 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 numbersx % 2 != 0
A const that is changedlet
A pub function that uses a capability without saying sothe 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 a let that 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.