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);
}
}
This diagnostic rule is classified as:
Was this page helpful?
Your message has been sent. We will email you at
If you do not see the email in your inbox, please check if it is filtered to one of the following folders: