-
Notifications
You must be signed in to change notification settings - Fork 1.7k
new lint: missing_asserts_for_indexing
#10692
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
Conversation
r? @Alexendoo (rustbot has picked a reviewer for you, use r? to override) |
Oh huh, I just tried adding a test for multiple different slices (ie slices from different bindings) in the same block: fn foo(a: &[u8], b: &[u8]) {
let _ = a[0] + a[1];
let _ = b[0] + b[1];
} and it seems like it thinks all of those array accesses reference the same slice. rust-clippy/clippy_utils/src/hir_utils.rs Lines 878 to 883 in 9283497
Not really sure why it is doing what it is doing (always hashing |
I think the problem is that if two idents are the same constant value, What we could do is hash locals to the constant value if any and else hash the ident, I think. |
☔ The latest upstream changes (presumably #10578) made this pull request unmergeable. Please resolve the merge conflicts. |
There'd be the case where locals are deeper in other expressions like |
missing_assert_for_indexing
missing_asserts_for_indexing
☔ The latest upstream changes (presumably #10716) made this pull request unmergeable. Please resolve the merge conflicts. |
Right now this only works if integer literals are used for indexing and it'd be interesting if this could be extended to also work for some simple arithmetic, like fn foo(x: &[i32], n: usize) -> i32 {
// suggest `assert!(x.len() > n + 3);` here
x[n] + x[n + 1] + x[n + 2] + x[n + 3]
} But that probably complicates the logic quite a bit, so maybe it's worth doing this in a separate PR? |
I'm not sure about the arithmetic case, does that version still get optimised to a single bounds check? In any case yeah let's keep it (relatively) simple to start with |
Nominating this for discussion in the next clippy meeting. Tuesday the 13th, 15:00 UTC @ https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy I'd like to discuss what category this should fit into |
Huh, good point. It doesn't look like it gets optimized to a single bounds check as I've written it with the assert. I am guessing that the fact that
👍 |
Changed the category to restriction as decided in the clippy meeting |
Thanks! LGTM with a rebase @bors delegate+ |
✌️ @y21, you can now approve this pull request! If @Alexendoo told you to " |
I rebased the PR and also had to change a few things in the tests since uitest has changed quite a bit since I've created this PR (intentionally put this into its own commit so reviewing that is easier). |
I wonder how that happened 😅 I really need to start going through my assigned PRs bottom to top so things don't get left like this Thanks again! @bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Fixes #8296
This lint looks for repeated slice indexing and suggests adding an
assert!
beforehand that helps LLVM elide bounds checks. The lint documentation has an example.I'm not really sure what category this should be in. It seems like a nice lint for the
perf
category but I suspect this has a pretty high FP rate, so it might have to be a pedantic lint or something.I'm also not sure about the name. If someone knows a better name for this lint, I'd be fine with changing it.
changelog: new lint [
missing_asserts_for_indexing
]