-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Lint uses of Vec
where arrays would do fine
#2262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Don't we already have this? https://rust-lang-nursery.github.io/rust-clippy/master/index.html#useless_vec Doesn't trigger in your case. So just an improvement to the existing lint required |
It should also catch cases like let x: i32 = vec![1,2,3].iter().sum(); |
I've been experimenting with the idea of improving the already existing
This already catches a lot of cases in the tests alone (61) and it's going to take a while to go through all of them. (Also, interestingly, the case @matthiaskrgr mentioned does not get caught, but changing it very slightly to immediately borrow the vec makes clippy catch it: playground) |
[`useless_vec`]: lint `vec!` invocations when a slice or an array would do First off, sorry for that large diff in tests. *A lot* of tests seem to trigger the lint with this new change, so I decided to `#![allow()]` the lint in the affected tests to make reviewing this easier, and also split the commits up so that the first commit is the actual logic of the lint and the second commit contains all the test changes. The stuff that changed in the tests is mostly just line numbers now. So, as large as the diff looks, it's not actually that bad. 😅 I manually went through all of these to find out about edge cases and decided to put them in `tests/ui/vec.rs`. For more context, I wrote about the idea of this PR here: #2262 (comment) (that explains the logic) Basically, it now also considers the case where a `Vec` is put in a local variable and the user only ever does things with it that one could also do with a slice or an array. This should catch a lot more cases, and (at least from looking at the tests) it does. changelog: [`useless_vec`]: lint `vec!` invocations when a slice or an array would do (also considering local variables now)
For example:
Bonus points: suggest using
ArrayVec
when the size is statically guaranteed to be less than a certain amount.The text was updated successfully, but these errors were encountered: