coding style – Is it possible to restructure code to avoid this copy-paste bug?

I came across quite a subtle bug the other day, where there are two sections of similar code that were supposed to use different variables, but copy-pasting had lead them to use the same variable. I’ve reduced it to the the example below.

static float module_variable;

float foo() {
    float a = calculate_a();
    float b = calculate_b();

    float c = calculate_c();
    float d = calculate_d();

    float output = get_default();

    if (a > some_threshold) {
         // `a` meets criteria, therefore we should use it
        output += a;
        module_variable = c;
    } else if (b < other_threshold) {
        // `b` meets criteria, therefore we should use it
        output += a;  // *BUG* we should be using `output += b` here
        module_variable = d;
    }

    return output
}

A lint for unused variables almost catches this, but because the else if uses b, the linter won’t complain.

It seems like I want a way to only allow the inner block access to variables that were used to get into that block.

This particular bug was in C, but I’d be interested to see if anyone can come up with a different way of structuring this code to avoid bugs like this (either in C or any other language).