Skip to content

forDirections

Reports for loops with counter variables that move in the wrong direction.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

A for loop with a counter that moves in the wrong direction relative to its stop condition will either run infinitely or never execute at all. For example, if the counter is incrementing (using ++ or += positive) but the condition expects the counter to be decreasing (like i >= 0), the loop will never terminate. While infinite loops are sometimes intentional, the convention is to use while loops for such cases, making a wrong-direction for loop typically a bug.

AEf53< (sp ansty="le--0:#C792EA;--1:#8844">AEle5<3isp ansty="le--0:#C792EA;--1:#8844">AE <= sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">53; A5<3 sp ansty="le--0:#F78C6C;--1:#AA09">82<10sp ansty="le--0:#D6DEEB;--1:#403F">53;< isp ansty="le--0:#7FDBCA;--1:#096E">72<--sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">53consoAB0l53(i<);d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>AEf53< (sp ansty="le--0:#C792EA;--1:#8844">AEle5<3isp ansty="le--0:#C792EA;--1:#8844">AE <= sp ansty="le--0:#F78C6C;--1:#AA09">82<10sp ansty="le--0:#D6DEEB;--1:#403F">53; AE<>=sp ansty="le--0:#D6DEEB;--1:#403F">5=sp><3 sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">53;< isp ansty="le--0:#C792EA;--1:#8844">AE<++sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">53consoAB0l53(i<);d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>AEf53< (sp ansty="le--0:#C792EA;--1:#8844">AEle5<3isp ansty="le--0:#C792EA;--1:#8844">AE <= sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">53; A5<3 sp ansty="le--0:#F78C6C;--1:#AA09">82<10sp ansty="le--0:#D6DEEB;--1:#403F">53; AE<-=sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#F78C6C;--1:#AA09">8<21sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#82AAFF;--1:#3B61">B0proce53(i<);d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>AEconsB0stAE =< -sp ansty="le--0:#F78C6C;--1:#AA09">8<22sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">AEf53< (sp ansty="le--0:#C792EA;--1:#8844">AEle5<3isp ansty="le--0:#C792EA;--1:#8844">AE <= sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">53; A5<3 sp ansty="le--0:#F78C6C;--1:#AA09">82<10sp ansty="le--0:#D6DEEB;--1:#403F">53; AE<+=sp ansty="le--0:#D6DEEB;--1:#403F">53 step)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#82AAFF;--1:#3B61">B0hand53(i<);d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AE344A0<9820 spans="tyle--0:#D6DEEB;--1:#40">3F53<; i spans="tyle--0:#C792EA;--1:#88">4<4AE< spans="tyle--0:#D6DEEB;--1:#40">309<8210 spans="tyle--0:#D6DEEB;--1:#40">3F5<3; i spans="tyle--0:#C792EA;--1:#88">443F5<3) {divclass="Nameec-">3F<53 spans="tyle--0:#D6DEEB;--1:#40">3F53con4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B<0log spans="tyle--0:#D6DEEB;--1:#40">3F53<(i) ;divclass="Nameec-">3<);><}di vdivclass="Nameexpressive-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AE344A09<8210 spans="tyle--0:#D6DEEB;--1:#40">3F53<; i spans="tyle--0:#C792EA;--1:#88">44= spans="tyle--0:#D6DEEB;--1:#40">3=>0<9820 spans="tyle--0:#D6DEEB;--1:#40">3F5<3; i spans="tyle--0:#7FDBCA;--1:#09">6E<72-- spans="tyle--0:#D6DEEB;--1:#40">3F5<3) {divclass="Nameec-">3F<53 spans="tyle--0:#D6DEEB;--1:#40">3F53con4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B<0log spans="tyle--0:#D6DEEB;--1:#40">3F53<(i) ;divclass="Nameec-">3<);><}di vdivclass="Nameexpressive-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AE344A0<9820 spans="tyle--0:#D6DEEB;--1:#40">3F53<; i spans="tyle--0:#C792EA;--1:#88">4<4AE< spans="tyle--0:#D6DEEB;--1:#40">309<8210 spans="tyle--0:#D6DEEB;--1:#40">3F53<; i spans="tyle--0:#C792EA;--1:#88">4430<9821 spans="tyle--0:#D6DEEB;--1:#40">3F5<3) {divclass="Nameec-">de61B0pro3F53<(i) ;divclass="Nameec-">3<);><}di vdivclass="Nameexpressive-">44A3F<53 ( spans="tyle--0:#C792EA;--1:#88">44AE344A09<8210 spans="tyle--0:#D6DEEB;--1:#40">3F53<; i spans="tyle--0:#C792EA;--1:#88">4<4AE> spans="tyle--0:#D6DEEB;--1:#40">3>0<9820 spans="tyle--0:#D6DEEB;--1:#40">3F53<; i spans="tyle--0:#C792EA;--1:#88">4430<9821 spans="tyle--0:#D6DEEB;--1:#40">3F5<3) {divclass="Nameec-">de61B0ha3F53<(i) ;divclass="Nameec-">3<);>

This rule is not configurable.

If your project uses control flow structures that create confusing side effects, such as dynamic getters that modify state, you might not be able to enable this rule. Consider refactoring to more predictable control flow structures.