Repository navigation
Add manual_as_slice lint - #14809
Add manual_as_slice lint#14809nils-degroot wants to merge 2 commits into
manual_as_slice lint#14809Conversation
llogiq
left a comment
There was a problem hiding this comment.
This looks like a good starting point. I'd want some more test cases, and there is some duplication we can remove, but otherwise this seems mostly merge-worthy. I'd like to rename the lint though, it's too wordy. How about manual_as_slice? That fits very well into our naming structure. Also please squash your commits when ready.
|
I like |
1a8a066 to
dc4e07b
Compare
|
I think you need to re-bless the tests. |
as_slice_instead_of_reference_full_range lintmanual_as_slice lint
samueltardieu
left a comment
There was a problem hiding this comment.
You should at least check that:
- the resulting type is a slice
- the
.as_slice()/.as_mut_slice()methods exist on the original type
or restrict the original type to those coming from core/alloc.
Otherwise, you take the risk of having a type implement [..] for something else (let's say, in a domain-specific-language) without having .as_slice() available. Here is an example:
#![feature(new_range_api)]
use std::ops::Index;
use std::range::RangeBounds;
struct Count;
impl<R: RangeBounds<()>> Index<R> for Count {
type Output = ();
fn index(&self, _: R) -> &Self::Output {
&()
}
}
fn main() {
_ = &Count[..];
}It will suggest to use Count.as_slice() even though this method does not exist on Count.
|
I'm gonna restrict is to Vec, array and slice in that case, since doing this on a string also would not work |
|
Is it possible to get this working on vec since its not a language item? |
Language items are items that the compiler needs when it transforms code. For example, it needs However, |
llogiq
left a comment
There was a problem hiding this comment.
I'd like the message to be shorter. Otherwise this looks mostly good. We may want to select a different lint group though. I'll start the final comment period soon.
545c550 to
a8a448d
Compare
This comment has been minimized.
This comment has been minimized.
|
Ping @nils-degroot are you willing to continue working on this? r? Jarcho |
|
Hey, forgot this was open. I'll handle the comments and rebase now |
750f2da to
dabee41
Compare
This comment has been minimized.
This comment has been minimized.
|
Lintcheck changes for 009e84d
This comment will be updated if you push new changes |
|
This lint has been nominated for inclusion. |
|
Reminder, once the PR becomes ready for a review, use |
dabee41 to
adf9001
Compare
d0aa664 to
2a52d1a
Compare
|
Thanks for the help 🙏, I've resolved the comments. Should I resolve the dogfood comments by running clippy fix? I can't really find documentation about whats normal for this |
|
Ah, that's a good point, we should have docs for this. In general, the dogfood comments should be resolved, yes – unless you notice a diagnostic which looks broken, in which case you should fix the lint instead. Please make a separate commit for the dogfood fixes (a description as simple as |
2a52d1a to
0d31da6
Compare
0d31da6 to
009e84d
Compare
|
Some changes occurred in clippy_lints/src/doc cc @notriddle |
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
| let slice: &[u8] = &object.field[..]; | ||
| //~^ manual_as_slice | ||
|
|
||
| // sized types in a box where the contents of the box is slice-like, shoudl emit the manual_as_slice |
There was a problem hiding this comment.
This comment applies to the case above as well I think? So maybe move it there, and also s/shoudl/should.
Btw, is there a reason to have these wrapper structs? Why not use Box<Vec<u8>> and Box<[u8]> directly?
|
|
||
| struct UnsizedBox(Box<[u8]>); | ||
| let object = UnsizedBox(Box::new([])); | ||
| // unsized slices don't have a as_slice implementations, so don't suggest the lint |
There was a problem hiding this comment.
| // unsized slices don't have a as_slice implementations, so don't suggest the lint | |
| // unsized slices don't have the `.as_slice()` method, so don't suggest the lint |
| @@ -0,0 +1,79 @@ | |||
| #![allow(clippy::box_collection, clippy::redundant_slicing)] | |||
There was a problem hiding this comment.
nit: I think you can expect box_collection instead of allowing, possibly the other lint as well. That's preferred because it would warn us when the expect is no longer needed
|
☔ The latest upstream changes (possibly #13598) made this pull request unmergeable. Please resolve the merge conflicts. |
View all comments
This pr adds the
manual_as_slicelint.closes: #7633
changelog: [
manual_as_slice]: new lint