Skip to content

Add manual_as_slice lint #14809

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

nils-degroot
Copy link

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

This pr adds the manual_as_slice lint.

closes: #7633


changelog: add [manual_as_slice] lint

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

@llogiq llogiq left a comment

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.

@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
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
Copy link
Contributor

llogiq commented May 16, 2025

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
Copy link
Member

@samueltardieu samueltardieu left a comment

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
Copy link
Author

nils-degroot commented May 18, 2025

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
Author

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

@samueltardieu
Copy link
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
Copy link
Contributor

@llogiq llogiq left a comment

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.

@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.

@rustbot
Copy link
Collaborator

rustbot commented Jun 5, 2025

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

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.

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