V7023. Suspicious use of a variable in similar code fragments. Perhaps this is a typo, and a different variable may have been intended.
The analyzer has detected two structurally similar code blocks following one another that differ by a single variable. In the first block, this variable appears multiple times, but in the second block it appears only once. Perhaps, the author wrote such code using copy‑paste and forgot to change the variable.
The example:
if (x > 0) {
Do1(x);
Do2(x);
}
if (y > 0) {
Do1(y);
Do2(x); // <=
}
In the first block, all actions involve the x variable. In the second block, all actions involve the y variable, except for the Do2(x)method. The author might have forgotten to change the variable, and the correct version should look like this:
if (x > 0) {
Do1(x);
Do2(x);
}
if (y > 0) {
Do1(y);
Do2(y);
}
A good practice that protects against such an error is to extract the code into separate functions:
processVariable(x);
processVariable(y);
function processVariable(value) {
if (value > 0) {
Do1(value);
Do2(value);
}
}