Skip to content
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

Merged
merged 1 commit into from
Nov 11, 2021

Conversation

xFrednet
Copy link
Member

@xFrednet xFrednet commented Sep 7, 2021

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 [index_refutable_slice]

I've already fixed a bunch of lint triggers in #7638 to make this PR smaller

Closes: #7569

@rust-highfive
Copy link

r? @camsteffen

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 7, 2021
@camsteffen
Copy link
Contributor

camsteffen commented Sep 7, 2021

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 :(");
    }
}

@xFrednet
Copy link
Member Author

xFrednet commented Sep 7, 2021

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 pedantic group. We could also set it to nursery for the start. I mainly wrote it with Clippy in mind xD

@camsteffen
Copy link
Contributor

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?

@camsteffen
Copy link
Contributor

I mainly wrote it with Clippy in mind xD

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.

@flip1995
Copy link
Member

flip1995 commented Sep 9, 2021

I like the idea of the lint. We could put it in nursery for now and try with the lintcheck tool how often it triggers in the wild.

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 if let. So I think the scope of this lint is an open question.

@xFrednet
Copy link
Member Author

xFrednet commented Sep 9, 2021

Okay, then I'll change the group to nursery.

The tests seem to only address cases where the slice is immediately indexed after the if let

The lint uses a UseExprVisitor to move through the entire block. The tests are rather simple, as you've noticed. I've already fixed the lint triggers inside Clippy in #7638 which displays some more complex example. I'll put it on the to-do list to add some more complex examples.

(Sorry, for not adding them sooner, my semester is currently taking up a lot of time. It should be better in the upcoming week 🙃 )

@camsteffen
Copy link
Contributor

Alright, maybe I'm being overly pessimistic. :)

I have an idea for naming the lint/config if you like it. index_refutable_slice as in "index a slice binding from a refutable pattern". This conveys that a let statement would never be linted. For the config, max_suggested_slice_pattern_length as I think this is more "to the point" of what we want to configure. It could be reused by another lint, but if not that's fine.

clippy_lints/src/avoidable_slice_indexing.rs Outdated Show resolved Hide resolved
clippy_lints/src/avoidable_slice_indexing.rs Outdated Show resolved Hide resolved
clippy_lints/src/avoidable_slice_indexing.rs Outdated Show resolved Hide resolved
clippy_lints/src/avoidable_slice_indexing.rs Outdated Show resolved Hide resolved
clippy_lints/src/avoidable_slice_indexing.rs Outdated Show resolved Hide resolved
clippy_lints/src/avoidable_slice_indexing.rs Outdated Show resolved Hide resolved
clippy_lints/src/avoidable_slice_indexing.rs Outdated Show resolved Hide resolved
@camsteffen camsteffen added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Sep 12, 2021
@xFrednet xFrednet force-pushed the 7569-splits-for-slices branch 2 times, most recently from 9075c9e to 5cd1765 Compare September 13, 2021 13:45
@xFrednet
Copy link
Member Author

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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Sep 13, 2021
@xFrednet
Copy link
Member Author

xFrednet commented Sep 13, 2021

I have an idea for naming the lint/config if you like it. index_refutable_slice as in "index a slice binding from a refutable pattern". This conveys that a let statement would never be linted. For the config, max_suggested_slice_pattern_length as I think this is more "to the point" of what we want to configure.

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.

@camsteffen
Copy link
Contributor

Nah go ahead.

@xFrednet
Copy link
Member Author

I've also rebased on upstream/master now 🙃

Copy link
Contributor

@camsteffen camsteffen left a 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.

clippy_lints/src/index_refutable_slice.rs Outdated Show resolved Hide resolved
clippy_lints/src/utils/conf.rs Outdated Show resolved Hide resolved
clippy_lints/src/index_refutable_slice.rs Outdated Show resolved Hide resolved
clippy_lints/src/index_refutable_slice.rs Outdated Show resolved Hide resolved
clippy_lints/src/index_refutable_slice.rs Outdated Show resolved Hide resolved
tests/ui/index_refutable_slice/if_let_slice_binding.stderr Outdated Show resolved Hide resolved
clippy_lints/src/index_refutable_slice.rs Outdated Show resolved Hide resolved
clippy_lints/src/index_refutable_slice.rs Outdated Show resolved Hide resolved
clippy_lints/src/index_refutable_slice.rs Outdated Show resolved Hide resolved
@xFrednet xFrednet added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Sep 14, 2021
@bors
Copy link
Collaborator

bors commented Sep 17, 2021

☔ The latest upstream changes (presumably #7653) made this pull request unmergeable. Please resolve the merge conflicts.

@xFrednet
Copy link
Member Author

Just a small note for triage: I'm currently quite busy, but I still plan to continue this PR. Thank you for the review 🙃

@giraffate
Copy link
Contributor

ping from triage @xFrednet. This is just a reminder.

@xFrednet
Copy link
Member Author

xFrednet commented Nov 3, 2021

I'm starting to revive this branch. I'll ping you and switch the labels once I'm done 🙃

@xFrednet
Copy link
Member Author

xFrednet commented Nov 3, 2021

Thank you for waiting on this review! This should now be ready for review again @camsteffen 🙃

FWIW it's okay by me to squash in changes when it's a significant refactor.

Good to know, I've squashed the changes from your review into the commit as well.

@xFrednet xFrednet added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Nov 3, 2021
Copy link
Contributor

@camsteffen camsteffen left a 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

clippy_lints/src/index_refutable_slice.rs Outdated Show resolved Hide resolved
clippy_lints/src/index_refutable_slice.rs Outdated Show resolved Hide resolved
clippy_lints/src/index_refutable_slice.rs Outdated Show resolved Hide resolved
clippy_lints/src/index_refutable_slice.rs Outdated Show resolved Hide resolved
clippy_lints/src/index_refutable_slice.rs Show resolved Hide resolved
clippy_lints/src/utils/conf.rs Outdated Show resolved Hide resolved
tests/ui-toml/min_rust_version/min_rust_version.rs Outdated Show resolved Hide resolved
@bors
Copy link
Collaborator

bors commented Nov 9, 2021

☔ The latest upstream changes (presumably #7944) made this pull request unmergeable. Please resolve the merge conflicts.

@camsteffen camsteffen added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Nov 10, 2021
@xFrednet xFrednet added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Nov 11, 2021
@xFrednet xFrednet force-pushed the 7569-splits-for-slices branch 2 times, most recently from ae84f3e to 9f612b0 Compare November 11, 2021 15:52
Copy link
Contributor

@camsteffen camsteffen left a 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
@xFrednet
Copy link
Member Author

Done! Thanks for the review!

@camsteffen
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Nov 11, 2021

📌 Commit e444cbe has been approved by camsteffen

bors added a commit that referenced this pull request Nov 11, 2021
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
Copy link
Collaborator

bors commented Nov 11, 2021

⌛ Testing commit e444cbe with merge 7c52841...

@camsteffen
Copy link
Contributor

@bors r- (let's fix the title and changelog with the changed name)

@xFrednet xFrednet changed the title New lint avoidable_slice_indexing to avoid slice indexing New lint index_refutable_slice to avoid slice indexing Nov 11, 2021
@camsteffen
Copy link
Contributor

Er maybe it's too late to r- 🤷 @bors r+

@bors
Copy link
Collaborator

bors commented Nov 11, 2021

📌 Commit e444cbe has been approved by camsteffen

@bors
Copy link
Collaborator

bors commented Nov 11, 2021

⌛ Testing commit e444cbe with merge 3d4d0cf...

@bors
Copy link
Collaborator

bors commented Nov 11, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: camsteffen
Pushing 3d4d0cf to master...

@bors bors merged commit 3d4d0cf into rust-lang:master Nov 11, 2021
@xFrednet xFrednet deleted the 7569-splits-for-slices branch November 11, 2021 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New lint: slice_indexing_after_deconstruction
7 participants