-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 index_refutable_slice
to avoid slice indexing
#7643
Conversation
r? @camsteffen (rust-highfive has picked a reviewer for you, use r? to override) |
255f0e5
to
83559b8
Compare
I'm afraid of false positives as this lint changes semantics. Maybe this is acceptable for a pedantic lint, but I'm not sure. It's like suspicious-pedantic... if let Some(slice) = opt {
if !slice.is_empty() {
println!("first: {}", slice[0]);
}
}
if let Some(slice) = opt {
if interesting_stuff(slice) {
// interesting_stuff never returns true for empty slices
println!("first: {}", slice[0]);
}
}
if let Some(slice) = foo.bar {
if foo.is_interesting {
// foo.is_interesting is only true if the slice is not empty
println!("first: {}", slice[0]);
} else {
println!("not interesting :(");
}
} |
I was also a bit afraid of FPs. The implementation is therefore quite cautious, it at least covers all cases that I could think of and should only lint if the slice is used for indexing. I remove it from the list if it's used in any other way, but FPs could maybe still happen. The first two cases should definitely be covered, the third one should work as well. I'll add these tests tomorrow and also try to run lintcheck with it 🙃 I would definitely not make this allow-by-default at the start. And even later I think it should be an opt-in like it is in the |
Yeah I'm wary of lints where we can never really be sure that we're giving a false positive. You can add heuristics to be cautious, but ultimately we can't know from static analysis why the coder might have certain assumptions about the length of the slice, and how those assumptions change at different points in the code. The heuristics remove false positives and add false negatives at the same time. I think the pedantic group allows for picky lints, but not necessarily more false positives. Maybe this can just go in nursery or restriction. @rust-lang/clippy thoughts? |
That's fine, but I also think the FP rate in Clippy will be lower than in most other domains. For example, we often have a slice of arguments and we simply expect the number of arguments to be one static value. |
I like the idea of the lint. We could put it in nursery for now and try with the I haven't read the code, so I don't know what the scope of patterns is this lint triggers on. The tests seem to only address cases where the slice is immediately indexed after the |
Okay, then I'll change the group to nursery.
The lint uses a (Sorry, for not adding them sooner, my semester is currently taking up a lot of time. It should be better in the upcoming week 🙃 ) |
Alright, maybe I'm being overly pessimistic. :) I have an idea for naming the lint/config if you like it. |
9075c9e
to
5cd1765
Compare
I've updated the code now, this should hopefully address all comments and make the pipeline green ^^. @rustbot label -S-waiting-on-author +S-waiting-on-review |
I missed this review comment earlier. I like the suggested names. Do you want to review the latest changes beforehand? As this refactoring will most likely break the review comment links from GitHub. |
Nah go ahead. |
1ed88fa
to
cc982bd
Compare
I've also rebased on |
cc982bd
to
6162ec8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW it's okay by me to squash in changes when it's a significant refactor.
tests/ui-toml/max_suggested_slice_pattern_length/avoidable_slice_indexing_limit.rs
Outdated
Show resolved
Hide resolved
☔ The latest upstream changes (presumably #7653) made this pull request unmergeable. Please resolve the merge conflicts. |
Just a small note for triage: I'm currently quite busy, but I still plan to continue this PR. Thank you for the review 🙃 |
ping from triage @xFrednet. This is just a reminder. |
I'm starting to revive this branch. I'll ping you and switch the labels once I'm done 🙃 |
cf7e0bc
to
776c868
Compare
Thank you for waiting on this review! This should now be ready for review again @camsteffen 🙃
Good to know, I've squashed the changes from your review into the commit as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some minor stuff
tests/ui-toml/max_suggested_slice_pattern_length/avoidable_slice_indexing_limit.rs
Outdated
Show resolved
Hide resolved
tests/ui-toml/max_suggested_slice_pattern_length/avoidable_slice_indexing_limit.rs
Outdated
Show resolved
Hide resolved
☔ The latest upstream changes (presumably #7944) made this pull request unmergeable. Please resolve the merge conflicts. |
d2a232f
to
820f242
Compare
ae84f3e
to
9f612b0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks! Please squash.
* Finding pattern slices for `avoidable_slice_indexing` * `avoidable_slice_indexing` analysing slice usage * Add configuration to `avoidable_slice_indexing` * Emitting `avoidable_slice_indexing` with suggestions * Dogfooding and fixing bugs * Add ui-toml test for `avoidable_slice_indexing` * Correctly suggest `ref` keywords for `avoidable_slice_indexing` * Test and document `mut` for `avoid_slice_indexing` * Handle macros with `avoidable_slice_indexing` lint * Ignore slices with sub patterns in `avoidable_slice_indexing` * Update lint description for `avoidable_slice_indexing` * Move `avoidable_slice_indexing` to nursery * Added more tests for `avoidable_slice_indexing` * Update documentation and message for `avoidable_slice_indexing` * Teach `avoidable_slice_indexing` about `HirId`s and `Visitors` * Rename lint to `index_refutable_slice` and connected config
9f612b0
to
e444cbe
Compare
Done! Thanks for the review! |
@bors r+ |
📌 Commit e444cbe has been approved by |
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
@bors r- (let's fix the title and changelog with the changed name) |
avoidable_slice_indexing
to avoid slice indexing index_refutable_slice
to avoid slice indexing
Er maybe it's too late to r- 🤷 @bors r+ |
📌 Commit e444cbe has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
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:
match
statementschangelog: New lint [
index_refutable_slice
]I've already fixed a bunch of lint triggers in #7638 to make this PR smaller
Closes: #7569