This rule prevents you from creating conditions in which a for loop might end up in an infinite loop. If you need an infinite loop, use while statements.
Non-Compliant Code Examples
// test if '++', '--'
for(vari=0;i<10;i--){}for(vari=0;i<=10;i--){}for(vari=10;i>10;i++){}for(vari=10;i>=0;i++){}// test if '+=', '-='
for(vari=0;i<10;i-=1){}for(vari=0;i<=10;i-=1){}for(vari=10;i>10;i+=1){}for(vari=10;i>=0;i+=1){}for(vari=0;i<10;i+=-1){}for(vari=0;i<=10;i+=-1){}for(vari=10;i>10;i-=-1){}for(vari=10;i>=0;i-=-1){}
Compliant Code Examples
// test if '++', '--'
for(vari=0;i<10;i++){}for(vari=0;i<=10;i++){}for(vari=10;i>0;i--){}for(vari=10;i>=0;i--){}// test if '+=', '-=',
for(vari=0;i<10;i+=1){}for(vari=0;i<=10;i+=1){}for(vari=0;i<10;i-=-1){}for(vari=0;i<=10;i-=-1){}for(vari=10;i>0;i-=1){}for(vari=10;i>=0;i-=1){}for(vari=10;i>0;i+=-1){}for(vari=10;i>=0;i+=-1){}// test if no update.
for(vari=10;i>0;){}for(vari=10;i>=0;){}for(vari=10;i<0;){}for(vari=10;i<=0;){}for(vari=10;i<=0;j++){}for(vari=10;i<=0;j--){}for(vari=10;i>=0;j++){}for(vari=10;i>=0;j--){}for(vari=10;i>=0;j+=2){}for(vari=10;i>=0;j-=2){}for(vari=10;i>=0;i|=2){}for(vari=10;i>=0;i%=2){}for(vari=0;i<MAX;i+=STEP_SIZE);for(vari=0;i<MAX;i-=STEP_SIZE);for(vari=10;i>0;i+=STEP_SIZE);// other cond-expressions.
for(vari=0;i!==10;i+=1){}for(vari=0;i===10;i+=1){}for(vari=0;i==10;i+=1){}for(vari=0;i!=10;i+=1){}
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- javascript-best-practices # Rules to enforce JavaScript best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines