L3989 False Positive in LoopsShouldNotBeInfiniteCheck: should support guard clause

  • SonarQube Enterprise Edition Version 8.9 (build 43852)
  • error message: “‘parent’ is not modified in this loop.”
var parent = $container.find('.list')[0];
while (parent && parent.firstChild) {
  parent.removeChild(parent.firstChild);
}

I’m not sure where this is going wrong, I think it doesn’t understand this is a guard clause checking parent for a null-ish value to prevent “Cannot read property ‘firstChild’ of undefined” exception if .find() return no elements.

Hello Donald,

Welcome to SonarSource community, and thank you for your feedback.

The false positive you are mentioning is not due to a missing support of guard class but because of a current limitation of the rule. In your case, the loop condition partially depends on a mutable object, parent, which is modified through calls to removeChild. However, the rule doesn’t consider mutation of loop condition variables through function or method calls, and therefore, raises a false positive.

I created the following ticket to address this false positive.

Best,
Yassin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.