mpeterv / luacheck

A tool for linting and static analysis of Lua code.
MIT License
1.92k stars 322 forks source link

Catch for loops counting up when they should be counting down #160

Closed Zash closed 6 years ago

Zash commented 6 years ago

Consider a for loop like this:

for i = #a, 1 do
  something(i)
end

This is very likely a mistake, and the intention was to iterate down over some array, ie:

for i = #a, 1, -1 do
  something(i)
end

Is this something luacheck can or should point out?

mpeterv commented 6 years ago

This sounds helpful, I've made mistakes like that one. Should be easy to implement, too, just go through the AST recursively looking for ForNum nodes with limits like that. Just need to figure out the warning code. Probably something in 5xx.

mpeterv commented 6 years ago

Implemented on master branch.