-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
New lint: slice_indexing_after_deconstruction
#7569
Comments
This comment has been minimized.
This comment has been minimized.
We have to pick some max threshold for indexes to lint. Like we don't want to lint |
Good suggestion, a threshold of 3 would catch all instance in Clippy that I've seen. We could also have this configurable and have the default be 3 👍. |
Reviewing a rustup PR I found this awesome refactoring which could also be suggested by this lint as an enhancement later on: // from
for win in blocks.windows(2) {
// to
let mut iter = blocks.windows(2);
while let Some(&[win0, win1]) = iter.next() { (Let's just ignore that this refactoring was done in my first bigger lint implementation xD) |
I'm holding out for |
That would be even nicer. I want to work on this once rustup is merged. It sounds like a useful and fun lint 🙃 @rustbot claim |
…hearth Avoid slice indexing in Clippy (down with the ICEs) While working on #7569 I got about 23 lint reports where we can avoid slice indexing by destructing the slice early. This is a preparation PR to avoid fixing them in the lint PR. (The implementation already takes about 300 lines without tests 😅). Either way, this should hopefully be easy to review 🙃 --- changelog: none
New lint `avoidable_slice_indexing` to avoid slice indexing A new lint to check for slices that could be deconstructed to avoid indexing. This lint should hopefully prevent some panics in other projects and ICEs for us. See #7569 for an example The implementation specifically checks for immutable bindings in `if let` statements to slices and arrays. Then it checks if these bindings are only used for value access using indices and that these indices are lower than the configured limit. I did my best to keep the implementation small, however the check was sadly quite complex. Now it's around 300 lines for the implementation and the rest are test. --- Optional future improvements: * Check for these instances also in `match` statements * Check for mutable slice bindings that could also be destructed --- changelog: New lint [`avoidable_slice_indexing`] I've already fixed a bunch of lint triggers in #7638 to make this PR smaller Closes: #7569
What it does
The lint checks for slices that come from value deconstruction and are only used to access individual slice values.
Categories (optional)
Advantages
Deconstruction of slices allows direct value access without using indexes on the slice, which can lead to panics. It also enables the user to give slice values a better name and indirectly check the correct length of the slice. See #7566 files as an example of how this can be an improvement.
Clippy already had a few instances where ICEs would have been prevented by using deconstruction of slices.
Example
Could be written as:
Notes:
if_chain
macros withif let
statements. I think it would be reasonable for this lint to also check if statements insideif_chain
macros.The text was updated successfully, but these errors were encountered: