Skip to content

Add manual_as_slice lint - #14809

Open
nils-degroot wants to merge 2 commits into
rust-lang:masterfrom
nils-degroot:master
Open

nils-degroot wants to merge 2 commits into
rust-lang:masterfrom
nils-degroot:master

Conversation

@nils-degroot

@nils-degroot nils-degroot commented May 15, 2025 •

Copy link
Copy Markdown

View all comments

This pr adds the manual_as_slice lint.

closes: #7633


changelog: [manual_as_slice]: new lint

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 15, 2025

@llogiq llogiq left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread clippy_lints/src/as_slice_instead_of_reference_full_range.rs Outdated
Comment thread tests/ui/as_slice_instead_of_reference_full_range.rs Outdated
@rustbot rustbot 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 May 16, 2025
@nils-degroot

Copy link
Copy Markdown
Author

I like manual_as_slice, it shows the intended behavior for the lint. I'll make the changes

@nils-degroot
nils-degroot force-pushed the master branch 2 times, most recently from 1a8a066 to dc4e07b Compare May 16, 2025 13:51
@llogiq

llogiq commented May 16, 2025

Copy link
Copy Markdown
Contributor

I think you need to re-bless the tests.

@llogiq llogiq changed the title changelog: Add as_slice_instead_of_reference_full_range lint Add manual_as_slice lint May 16, 2025
Comment thread tests/ui/manual_as_slice.fixed Outdated

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@nils-degroot

nils-degroot commented May 18, 2025 •

Copy link
Copy Markdown
Author

I'm gonna restrict is to Vec, array and slice in that case, since doing this on a string also would not work

@nils-degroot

Copy link
Copy Markdown
Author

Is it possible to get this working on vec since its not a language item?

@samueltardieu

Copy link
Copy Markdown
Member

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 panic!() as a language item, as it will generate calls to it. alloc::vec::Vec is not in this category, as the compiler never needs to transform the code in a way that would build a vector.

However, Vec is a diagnostic item, which means that you can get methods from clippy_utils (look for "diagnostic" there) to identify whether something is a Vec or not. Most of the things Clippy needs to identify in the standard library are marked as diagnostic items, and we can mark new items if we need them, so that we can easily use them in lints.

@nils-degroot
nils-degroot requested a review from llogiq May 27, 2025 05:36
@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 May 27, 2025

@llogiq llogiq left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread clippy_lints/src/manual_as_slice.rs Outdated
@nils-degroot
nils-degroot force-pushed the master branch 3 times, most recently from 545c550 to a8a448d Compare May 31, 2025 06:45
@rustbot

This comment has been minimized.

@Jarcho

Jarcho commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Ping @nils-degroot are you willing to continue working on this?

r? Jarcho

@rustbot rustbot assigned Jarcho and unassigned llogiq Sep 2, 2026
@nils-degroot

Copy link
Copy Markdown
Author

Hey, forgot this was open. I'll handle the comments and rebase now

@rustbot rustbot added the needs-fcp PRs that add, remove, or rename lints and need an FCP label Sep 16, 2026
@rustbot

This comment has been minimized.

@nils-degroot
nils-degroot requested a review from ada4a September 16, 2026 19:21
@github-actions

github-actions Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Lintcheck changes for 009e84d

Lint Added Removed Changed
clippy::manual_as_slice 204 0 0

This comment will be updated if you push new changes

@ada4a ada4a left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread clippy_lints/src/manual_as_slice.rs Outdated
Comment thread clippy_lints/src/manual_as_slice.rs Outdated
Comment thread clippy_lints/src/manual_as_slice.rs Outdated
Comment thread clippy_lints/src/manual_as_slice.rs Outdated
Comment thread clippy_lints/src/manual_as_slice.rs Outdated
Comment thread clippy_lints/src/manual_as_slice.rs
Comment thread clippy_lints/src/manual_as_slice.rs
@rustbot rustbot added lint-nominated Create an FCP-thread on Zulip for this PR and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Sep 17, 2026
@rustbot

rustbot commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

This lint has been nominated for inclusion.

A FCP topic has been created on Zulip.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Sep 17, 2026
@rustbot

rustbot commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@ada4a ada4a left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some nits, looks great otherwise:)

View changes since this review

Comment thread clippy_lints/src/manual_as_slice.rs Outdated
Comment thread clippy_lints/src/manual_as_slice.rs
Comment thread clippy_lints/src/manual_as_slice.rs Outdated
Comment thread tests/ui/manual_as_slice.rs
Comment thread tests/ui/manual_as_slice.rs
Comment thread tests/ui/manual_as_slice.rs
Comment thread clippy_lints/src/manual_as_slice.rs
@nils-degroot
nils-degroot force-pushed the master branch 2 times, most recently from d0aa664 to 2a52d1a Compare September 23, 2026 18:00
@nils-degroot

Copy link
Copy Markdown
Author

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

@ada4a

ada4a commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

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 fix dogfood would be fine), and have it come before the commit that actually implements the lint – this helps with reverting and whatnot.

@rustbot

rustbot commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in clippy_lints/src/doc

cc @notriddle

@rustbot

rustbot commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

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

@ada4a ada4a Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

View changes since the review


struct UnsizedBox(Box<[u8]>);
let object = UnsizedBox(Box::new([]));
// unsized slices don't have a as_slice implementations, so don't suggest the lint

@ada4a ada4a Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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

View changes since the review

@@ -0,0 +1,79 @@
#![allow(clippy::box_collection, clippy::redundant_slicing)]

@ada4a ada4a Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

View changes since the review

@rustbot

rustbot commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lint-nominated Create an FCP-thread on Zulip for this PR needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest usage of array.as_slice() (instead of &array[..]) and array.as_mut_slice()

6 participants