Kampfkarren / selene

A blazing-fast modern Lua linter written in Rust
https://kampfkarren.github.io/selene/
Mozilla Public License 2.0
591 stars 75 forks source link

Lint `math.random(1, #x)` in favor of `math.random(#x)` #393

Open Kampfkarren opened 2 years ago

Kampfkarren commented 2 years ago

More concise.

sisco0 commented 1 year ago

@Kampfkarren, considering the given PR (#442), could you kindly set a review over it? It seems that might be we are missing the fact that Math.random could be also called without any parameter for generating a pseudorandom floating point number as mentioned in the Lua Documentation.

Kampfkarren commented 1 year ago

This isn't quite what I meant. math.random() is fine, math.random(5) is fine, and math.random(2, 5) is fine, but math.random(1, 5) is the same as math.random(5), and should be linted as such.

Kampfkarren commented 1 year ago

I suspect you're going through "good first issue", which is great! This one is gonna be more complicated than the other two--it's a good introduction if you want to get into writing lints and traversing ASTs, but is not trivial.

sisco0 commented 1 year ago

By setting a review on the given documentation and current rules files, I got to the conclusion that an if_chain should be created with the following elements:

  1. If the current instruction is a call to math.random
  2. If the number of arguments is 2
  3. If the first argument is 1

Thank you for sharing this content with me and helping me to get into development of this application. I would take a more in-depth view and local experimentation before proposing further solutions.

Kampfkarren commented 1 year ago

That's correct, with the extra edge case of making sure the user doesn't define math. You can do this with the ScopeManager utility, look around for how that's done for other lints.